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. 25*33cde0d0SMatthew Ahrens * Copyright (c) 2011, 2014 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. 30fa9e4066Sahrens */ 31fa9e4066Sahrens 32fa9e4066Sahrens #include <ctype.h> 33fa9e4066Sahrens #include <errno.h> 34fa9e4066Sahrens #include <libintl.h> 35fa9e4066Sahrens #include <math.h> 36fa9e4066Sahrens #include <stdio.h> 37fa9e4066Sahrens #include <stdlib.h> 38fa9e4066Sahrens #include <strings.h> 39fa9e4066Sahrens #include <unistd.h> 403cb34c60Sahrens #include <stddef.h> 41fa9e4066Sahrens #include <zone.h> 4299653d4eSeschrock #include <fcntl.h> 43fa9e4066Sahrens #include <sys/mntent.h> 44b12a1c38Slling #include <sys/mount.h> 45ecd6cf80Smarks #include <priv.h> 46ecd6cf80Smarks #include <pwd.h> 47ecd6cf80Smarks #include <grp.h> 48ecd6cf80Smarks #include <stddef.h> 49ecd6cf80Smarks #include <ucred.h> 5014843421SMatthew Ahrens #include <idmap.h> 5114843421SMatthew Ahrens #include <aclutils.h> 523b12c289SMatthew Ahrens #include <directory.h> 53fa9e4066Sahrens 54c1449561SEric Taylor #include <sys/dnode.h> 55fa9e4066Sahrens #include <sys/spa.h> 56e9dbad6fSeschrock #include <sys/zap.h> 57fa9e4066Sahrens #include <libzfs.h> 58fa9e4066Sahrens 59fa9e4066Sahrens #include "zfs_namecheck.h" 60fa9e4066Sahrens #include "zfs_prop.h" 61fa9e4066Sahrens #include "libzfs_impl.h" 62ecd6cf80Smarks #include "zfs_deleg.h" 63fa9e4066Sahrens 6414843421SMatthew Ahrens static int userquota_propname_decode(const char *propname, boolean_t zoned, 6514843421SMatthew Ahrens zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp); 66cdf5b4caSmmusante 67fa9e4066Sahrens /* 68fa9e4066Sahrens * Given a single type (not a mask of types), return the type in a human 69fa9e4066Sahrens * readable form. 70fa9e4066Sahrens */ 71fa9e4066Sahrens const char * 72fa9e4066Sahrens zfs_type_to_name(zfs_type_t type) 73fa9e4066Sahrens { 74fa9e4066Sahrens switch (type) { 75fa9e4066Sahrens case ZFS_TYPE_FILESYSTEM: 76fa9e4066Sahrens return (dgettext(TEXT_DOMAIN, "filesystem")); 77fa9e4066Sahrens case ZFS_TYPE_SNAPSHOT: 78fa9e4066Sahrens return (dgettext(TEXT_DOMAIN, "snapshot")); 79fa9e4066Sahrens case ZFS_TYPE_VOLUME: 80fa9e4066Sahrens return (dgettext(TEXT_DOMAIN, "volume")); 81fa9e4066Sahrens } 82fa9e4066Sahrens 83fa9e4066Sahrens return (NULL); 84fa9e4066Sahrens } 85fa9e4066Sahrens 86fa9e4066Sahrens /* 87fa9e4066Sahrens * Given a path and mask of ZFS types, return a string describing this dataset. 88fa9e4066Sahrens * This is used when we fail to open a dataset and we cannot get an exact type. 89fa9e4066Sahrens * We guess what the type would have been based on the path and the mask of 90fa9e4066Sahrens * acceptable types. 91fa9e4066Sahrens */ 92fa9e4066Sahrens static const char * 93fa9e4066Sahrens path_to_str(const char *path, int types) 94fa9e4066Sahrens { 95fa9e4066Sahrens /* 96fa9e4066Sahrens * When given a single type, always report the exact type. 97fa9e4066Sahrens */ 98fa9e4066Sahrens if (types == ZFS_TYPE_SNAPSHOT) 99fa9e4066Sahrens return (dgettext(TEXT_DOMAIN, "snapshot")); 100fa9e4066Sahrens if (types == ZFS_TYPE_FILESYSTEM) 101fa9e4066Sahrens return (dgettext(TEXT_DOMAIN, "filesystem")); 102fa9e4066Sahrens if (types == ZFS_TYPE_VOLUME) 103fa9e4066Sahrens return (dgettext(TEXT_DOMAIN, "volume")); 104fa9e4066Sahrens 105fa9e4066Sahrens /* 106fa9e4066Sahrens * The user is requesting more than one type of dataset. If this is the 107fa9e4066Sahrens * case, consult the path itself. If we're looking for a snapshot, and 108fa9e4066Sahrens * a '@' is found, then report it as "snapshot". Otherwise, remove the 109fa9e4066Sahrens * snapshot attribute and try again. 110fa9e4066Sahrens */ 111fa9e4066Sahrens if (types & ZFS_TYPE_SNAPSHOT) { 112fa9e4066Sahrens if (strchr(path, '@') != NULL) 113fa9e4066Sahrens return (dgettext(TEXT_DOMAIN, "snapshot")); 114fa9e4066Sahrens return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT)); 115fa9e4066Sahrens } 116fa9e4066Sahrens 117fa9e4066Sahrens /* 118fa9e4066Sahrens * The user has requested either filesystems or volumes. 119fa9e4066Sahrens * We have no way of knowing a priori what type this would be, so always 120fa9e4066Sahrens * report it as "filesystem" or "volume", our two primitive types. 121fa9e4066Sahrens */ 122fa9e4066Sahrens if (types & ZFS_TYPE_FILESYSTEM) 123fa9e4066Sahrens return (dgettext(TEXT_DOMAIN, "filesystem")); 124fa9e4066Sahrens 125fa9e4066Sahrens assert(types & ZFS_TYPE_VOLUME); 126fa9e4066Sahrens return (dgettext(TEXT_DOMAIN, "volume")); 127fa9e4066Sahrens } 128fa9e4066Sahrens 129fa9e4066Sahrens /* 130fa9e4066Sahrens * Validate a ZFS path. This is used even before trying to open the dataset, to 13114843421SMatthew Ahrens * provide a more meaningful error message. We call zfs_error_aux() to 13214843421SMatthew Ahrens * explain exactly why the name was not valid. 133fa9e4066Sahrens */ 13499d5e173STim Haley int 135f18faf3fSek110237 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type, 136f18faf3fSek110237 boolean_t modifying) 137fa9e4066Sahrens { 138fa9e4066Sahrens namecheck_err_t why; 139fa9e4066Sahrens char what; 140fa9e4066Sahrens 1411af68beaSAlexander Stetsenko (void) zfs_prop_get_table(); 142fa9e4066Sahrens if (dataset_namecheck(path, &why, &what) != 0) { 14399653d4eSeschrock if (hdl != NULL) { 144fa9e4066Sahrens switch (why) { 145b81d61a6Slling case NAME_ERR_TOOLONG: 14699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 14799653d4eSeschrock "name is too long")); 148b81d61a6Slling break; 149b81d61a6Slling 150fa9e4066Sahrens case NAME_ERR_LEADING_SLASH: 15199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 15299653d4eSeschrock "leading slash in name")); 153fa9e4066Sahrens break; 154fa9e4066Sahrens 155fa9e4066Sahrens case NAME_ERR_EMPTY_COMPONENT: 15699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 15799653d4eSeschrock "empty component in name")); 158fa9e4066Sahrens break; 159fa9e4066Sahrens 160fa9e4066Sahrens case NAME_ERR_TRAILING_SLASH: 16199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 16299653d4eSeschrock "trailing slash in name")); 163fa9e4066Sahrens break; 164fa9e4066Sahrens 165fa9e4066Sahrens case NAME_ERR_INVALCHAR: 16699653d4eSeschrock zfs_error_aux(hdl, 167fa9e4066Sahrens dgettext(TEXT_DOMAIN, "invalid character " 16899653d4eSeschrock "'%c' in name"), what); 169fa9e4066Sahrens break; 170fa9e4066Sahrens 171fa9e4066Sahrens case NAME_ERR_MULTIPLE_AT: 17299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 17399653d4eSeschrock "multiple '@' delimiters in name")); 174fa9e4066Sahrens break; 1755ad82045Snd150628 1765ad82045Snd150628 case NAME_ERR_NOLETTER: 1775ad82045Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1785ad82045Snd150628 "pool doesn't begin with a letter")); 1795ad82045Snd150628 break; 1805ad82045Snd150628 1815ad82045Snd150628 case NAME_ERR_RESERVED: 1825ad82045Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1835ad82045Snd150628 "name is reserved")); 1845ad82045Snd150628 break; 1855ad82045Snd150628 1865ad82045Snd150628 case NAME_ERR_DISKLIKE: 1875ad82045Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1885ad82045Snd150628 "reserved disk name")); 1895ad82045Snd150628 break; 190fa9e4066Sahrens } 191fa9e4066Sahrens } 192fa9e4066Sahrens 193fa9e4066Sahrens return (0); 194fa9e4066Sahrens } 195fa9e4066Sahrens 196fa9e4066Sahrens if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) { 19799653d4eSeschrock if (hdl != NULL) 19899653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 19999653d4eSeschrock "snapshot delimiter '@' in filesystem name")); 200fa9e4066Sahrens return (0); 201fa9e4066Sahrens } 202fa9e4066Sahrens 2031d452cf5Sahrens if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) { 2041d452cf5Sahrens if (hdl != NULL) 2051d452cf5Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 206d7d4af51Smmusante "missing '@' delimiter in snapshot name")); 2071d452cf5Sahrens return (0); 2081d452cf5Sahrens } 2091d452cf5Sahrens 210f18faf3fSek110237 if (modifying && strchr(path, '%') != NULL) { 211f18faf3fSek110237 if (hdl != NULL) 212f18faf3fSek110237 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 213f18faf3fSek110237 "invalid character %c in name"), '%'); 214f18faf3fSek110237 return (0); 215f18faf3fSek110237 } 216f18faf3fSek110237 21799653d4eSeschrock return (-1); 218fa9e4066Sahrens } 219fa9e4066Sahrens 220fa9e4066Sahrens int 221fa9e4066Sahrens zfs_name_valid(const char *name, zfs_type_t type) 222fa9e4066Sahrens { 223e7cbe64fSgw25295 if (type == ZFS_TYPE_POOL) 224e7cbe64fSgw25295 return (zpool_name_valid(NULL, B_FALSE, name)); 225f18faf3fSek110237 return (zfs_validate_name(NULL, name, type, B_FALSE)); 226fa9e4066Sahrens } 227fa9e4066Sahrens 228fa9e4066Sahrens /* 229e9dbad6fSeschrock * This function takes the raw DSL properties, and filters out the user-defined 230e9dbad6fSeschrock * properties into a separate nvlist. 231e9dbad6fSeschrock */ 232fac3008cSeschrock static nvlist_t * 233fac3008cSeschrock process_user_props(zfs_handle_t *zhp, nvlist_t *props) 234e9dbad6fSeschrock { 235e9dbad6fSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 236e9dbad6fSeschrock nvpair_t *elem; 237e9dbad6fSeschrock nvlist_t *propval; 238fac3008cSeschrock nvlist_t *nvl; 239e9dbad6fSeschrock 240fac3008cSeschrock if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) { 241fac3008cSeschrock (void) no_memory(hdl); 242fac3008cSeschrock return (NULL); 243fac3008cSeschrock } 244e9dbad6fSeschrock 245e9dbad6fSeschrock elem = NULL; 246fac3008cSeschrock while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 247e9dbad6fSeschrock if (!zfs_prop_user(nvpair_name(elem))) 248e9dbad6fSeschrock continue; 249e9dbad6fSeschrock 250e9dbad6fSeschrock verify(nvpair_value_nvlist(elem, &propval) == 0); 251fac3008cSeschrock if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) { 252fac3008cSeschrock nvlist_free(nvl); 253fac3008cSeschrock (void) no_memory(hdl); 254fac3008cSeschrock return (NULL); 255fac3008cSeschrock } 256e9dbad6fSeschrock } 257e9dbad6fSeschrock 258fac3008cSeschrock return (nvl); 259e9dbad6fSeschrock } 260e9dbad6fSeschrock 26129ab75c9Srm160521 static zpool_handle_t * 26229ab75c9Srm160521 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name) 26329ab75c9Srm160521 { 26429ab75c9Srm160521 libzfs_handle_t *hdl = zhp->zfs_hdl; 26529ab75c9Srm160521 zpool_handle_t *zph; 26629ab75c9Srm160521 26729ab75c9Srm160521 if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) { 26829ab75c9Srm160521 if (hdl->libzfs_pool_handles != NULL) 26929ab75c9Srm160521 zph->zpool_next = hdl->libzfs_pool_handles; 27029ab75c9Srm160521 hdl->libzfs_pool_handles = zph; 27129ab75c9Srm160521 } 27229ab75c9Srm160521 return (zph); 27329ab75c9Srm160521 } 27429ab75c9Srm160521 27529ab75c9Srm160521 static zpool_handle_t * 27629ab75c9Srm160521 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len) 27729ab75c9Srm160521 { 27829ab75c9Srm160521 libzfs_handle_t *hdl = zhp->zfs_hdl; 27929ab75c9Srm160521 zpool_handle_t *zph = hdl->libzfs_pool_handles; 28029ab75c9Srm160521 28129ab75c9Srm160521 while ((zph != NULL) && 28229ab75c9Srm160521 (strncmp(pool_name, zpool_get_name(zph), len) != 0)) 28329ab75c9Srm160521 zph = zph->zpool_next; 28429ab75c9Srm160521 return (zph); 28529ab75c9Srm160521 } 28629ab75c9Srm160521 28729ab75c9Srm160521 /* 28829ab75c9Srm160521 * Returns a handle to the pool that contains the provided dataset. 28929ab75c9Srm160521 * If a handle to that pool already exists then that handle is returned. 29029ab75c9Srm160521 * Otherwise, a new handle is created and added to the list of handles. 29129ab75c9Srm160521 */ 29229ab75c9Srm160521 static zpool_handle_t * 29329ab75c9Srm160521 zpool_handle(zfs_handle_t *zhp) 29429ab75c9Srm160521 { 29529ab75c9Srm160521 char *pool_name; 29629ab75c9Srm160521 int len; 29729ab75c9Srm160521 zpool_handle_t *zph; 29829ab75c9Srm160521 29978f17100SMatthew Ahrens len = strcspn(zhp->zfs_name, "/@#") + 1; 30029ab75c9Srm160521 pool_name = zfs_alloc(zhp->zfs_hdl, len); 30129ab75c9Srm160521 (void) strlcpy(pool_name, zhp->zfs_name, len); 30229ab75c9Srm160521 30329ab75c9Srm160521 zph = zpool_find_handle(zhp, pool_name, len); 30429ab75c9Srm160521 if (zph == NULL) 30529ab75c9Srm160521 zph = zpool_add_handle(zhp, pool_name); 30629ab75c9Srm160521 30729ab75c9Srm160521 free(pool_name); 30829ab75c9Srm160521 return (zph); 30929ab75c9Srm160521 } 31029ab75c9Srm160521 31129ab75c9Srm160521 void 31229ab75c9Srm160521 zpool_free_handles(libzfs_handle_t *hdl) 31329ab75c9Srm160521 { 31429ab75c9Srm160521 zpool_handle_t *next, *zph = hdl->libzfs_pool_handles; 31529ab75c9Srm160521 31629ab75c9Srm160521 while (zph != NULL) { 31729ab75c9Srm160521 next = zph->zpool_next; 31829ab75c9Srm160521 zpool_close(zph); 31929ab75c9Srm160521 zph = next; 32029ab75c9Srm160521 } 32129ab75c9Srm160521 hdl->libzfs_pool_handles = NULL; 32229ab75c9Srm160521 } 32329ab75c9Srm160521 324e9dbad6fSeschrock /* 325fa9e4066Sahrens * Utility function to gather stats (objset and zpl) for the given object. 326fa9e4066Sahrens */ 327fa9e4066Sahrens static int 328ebedde84SEric Taylor get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc) 329fa9e4066Sahrens { 330e9dbad6fSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 331fa9e4066Sahrens 332ebedde84SEric Taylor (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name)); 333fa9e4066Sahrens 334ebedde84SEric Taylor while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) { 3357f7322feSeschrock if (errno == ENOMEM) { 336ebedde84SEric Taylor if (zcmd_expand_dst_nvlist(hdl, zc) != 0) { 33799653d4eSeschrock return (-1); 338e9dbad6fSeschrock } 3397f7322feSeschrock } else { 340fa9e4066Sahrens return (-1); 3417f7322feSeschrock } 3427f7322feSeschrock } 343ebedde84SEric Taylor return (0); 344fac3008cSeschrock } 345fac3008cSeschrock 34692241e0bSTom Erickson /* 34792241e0bSTom Erickson * Utility function to get the received properties of the given object. 34892241e0bSTom Erickson */ 34992241e0bSTom Erickson static int 35092241e0bSTom Erickson get_recvd_props_ioctl(zfs_handle_t *zhp) 35192241e0bSTom Erickson { 35292241e0bSTom Erickson libzfs_handle_t *hdl = zhp->zfs_hdl; 35392241e0bSTom Erickson nvlist_t *recvdprops; 35492241e0bSTom Erickson zfs_cmd_t zc = { 0 }; 35592241e0bSTom Erickson int err; 35692241e0bSTom Erickson 35792241e0bSTom Erickson if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) 35892241e0bSTom Erickson return (-1); 35992241e0bSTom Erickson 36092241e0bSTom Erickson (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 36192241e0bSTom Erickson 36292241e0bSTom Erickson while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) { 36392241e0bSTom Erickson if (errno == ENOMEM) { 36492241e0bSTom Erickson if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 36592241e0bSTom Erickson return (-1); 36692241e0bSTom Erickson } 36792241e0bSTom Erickson } else { 36892241e0bSTom Erickson zcmd_free_nvlists(&zc); 36992241e0bSTom Erickson return (-1); 37092241e0bSTom Erickson } 37192241e0bSTom Erickson } 37292241e0bSTom Erickson 37392241e0bSTom Erickson err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops); 37492241e0bSTom Erickson zcmd_free_nvlists(&zc); 37592241e0bSTom Erickson if (err != 0) 37692241e0bSTom Erickson return (-1); 37792241e0bSTom Erickson 37892241e0bSTom Erickson nvlist_free(zhp->zfs_recvd_props); 37992241e0bSTom Erickson zhp->zfs_recvd_props = recvdprops; 38092241e0bSTom Erickson 38192241e0bSTom Erickson return (0); 38292241e0bSTom Erickson } 38392241e0bSTom Erickson 384ebedde84SEric Taylor static int 385ebedde84SEric Taylor put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc) 386ebedde84SEric Taylor { 387ebedde84SEric Taylor nvlist_t *allprops, *userprops; 388ebedde84SEric Taylor 389ebedde84SEric Taylor zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */ 390ebedde84SEric Taylor 391ebedde84SEric Taylor if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) { 392ebedde84SEric Taylor return (-1); 393ebedde84SEric Taylor } 394fac3008cSeschrock 39514843421SMatthew Ahrens /* 39614843421SMatthew Ahrens * XXX Why do we store the user props separately, in addition to 39714843421SMatthew Ahrens * storing them in zfs_props? 39814843421SMatthew Ahrens */ 399fac3008cSeschrock if ((userprops = process_user_props(zhp, allprops)) == NULL) { 400fac3008cSeschrock nvlist_free(allprops); 401fac3008cSeschrock return (-1); 402fac3008cSeschrock } 403fac3008cSeschrock 40499653d4eSeschrock nvlist_free(zhp->zfs_props); 405fac3008cSeschrock nvlist_free(zhp->zfs_user_props); 40699653d4eSeschrock 407fac3008cSeschrock zhp->zfs_props = allprops; 408fac3008cSeschrock zhp->zfs_user_props = userprops; 40999653d4eSeschrock 410fa9e4066Sahrens return (0); 411fa9e4066Sahrens } 412fa9e4066Sahrens 413ebedde84SEric Taylor static int 414ebedde84SEric Taylor get_stats(zfs_handle_t *zhp) 415ebedde84SEric Taylor { 416ebedde84SEric Taylor int rc = 0; 417ebedde84SEric Taylor zfs_cmd_t zc = { 0 }; 418ebedde84SEric Taylor 419ebedde84SEric Taylor if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 420ebedde84SEric Taylor return (-1); 421ebedde84SEric Taylor if (get_stats_ioctl(zhp, &zc) != 0) 422ebedde84SEric Taylor rc = -1; 423ebedde84SEric Taylor else if (put_stats_zhdl(zhp, &zc) != 0) 424ebedde84SEric Taylor rc = -1; 425ebedde84SEric Taylor zcmd_free_nvlists(&zc); 426ebedde84SEric Taylor return (rc); 427ebedde84SEric Taylor } 428ebedde84SEric Taylor 429fa9e4066Sahrens /* 430fa9e4066Sahrens * Refresh the properties currently stored in the handle. 431fa9e4066Sahrens */ 432fa9e4066Sahrens void 433fa9e4066Sahrens zfs_refresh_properties(zfs_handle_t *zhp) 434fa9e4066Sahrens { 435fa9e4066Sahrens (void) get_stats(zhp); 436fa9e4066Sahrens } 437fa9e4066Sahrens 438fa9e4066Sahrens /* 439fa9e4066Sahrens * Makes a handle from the given dataset name. Used by zfs_open() and 440fa9e4066Sahrens * zfs_iter_* to create child handles on the fly. 441fa9e4066Sahrens */ 442ebedde84SEric Taylor static int 443ebedde84SEric Taylor make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc) 444fa9e4066Sahrens { 445503ad85cSMatthew Ahrens if (put_stats_zhdl(zhp, zc) != 0) 446ebedde84SEric Taylor return (-1); 44731fd60d3Sahrens 448fa9e4066Sahrens /* 449fa9e4066Sahrens * We've managed to open the dataset and gather statistics. Determine 450fa9e4066Sahrens * the high-level type. 451fa9e4066Sahrens */ 452a2eea2e1Sahrens if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) 453a2eea2e1Sahrens zhp->zfs_head_type = ZFS_TYPE_VOLUME; 454a2eea2e1Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) 455a2eea2e1Sahrens zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM; 456a2eea2e1Sahrens else 457a2eea2e1Sahrens abort(); 458a2eea2e1Sahrens 459fa9e4066Sahrens if (zhp->zfs_dmustats.dds_is_snapshot) 460fa9e4066Sahrens zhp->zfs_type = ZFS_TYPE_SNAPSHOT; 461fa9e4066Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) 462fa9e4066Sahrens zhp->zfs_type = ZFS_TYPE_VOLUME; 463fa9e4066Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) 464fa9e4066Sahrens zhp->zfs_type = ZFS_TYPE_FILESYSTEM; 465fa9e4066Sahrens else 46699653d4eSeschrock abort(); /* we should never see any other types */ 467fa9e4066Sahrens 4689fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL) 4699fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States return (-1); 4709fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 471ebedde84SEric Taylor return (0); 472ebedde84SEric Taylor } 473ebedde84SEric Taylor 474ebedde84SEric Taylor zfs_handle_t * 475ebedde84SEric Taylor make_dataset_handle(libzfs_handle_t *hdl, const char *path) 476ebedde84SEric Taylor { 477ebedde84SEric Taylor zfs_cmd_t zc = { 0 }; 478ebedde84SEric Taylor 479ebedde84SEric Taylor zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 480ebedde84SEric Taylor 481ebedde84SEric Taylor if (zhp == NULL) 482ebedde84SEric Taylor return (NULL); 483ebedde84SEric Taylor 484ebedde84SEric Taylor zhp->zfs_hdl = hdl; 485ebedde84SEric Taylor (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name)); 486ebedde84SEric Taylor if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) { 487ebedde84SEric Taylor free(zhp); 488ebedde84SEric Taylor return (NULL); 489ebedde84SEric Taylor } 490ebedde84SEric Taylor if (get_stats_ioctl(zhp, &zc) == -1) { 491ebedde84SEric Taylor zcmd_free_nvlists(&zc); 492ebedde84SEric Taylor free(zhp); 493ebedde84SEric Taylor return (NULL); 494ebedde84SEric Taylor } 495ebedde84SEric Taylor if (make_dataset_handle_common(zhp, &zc) == -1) { 496ebedde84SEric Taylor free(zhp); 497ebedde84SEric Taylor zhp = NULL; 498ebedde84SEric Taylor } 499ebedde84SEric Taylor zcmd_free_nvlists(&zc); 500ebedde84SEric Taylor return (zhp); 501ebedde84SEric Taylor } 502ebedde84SEric Taylor 50319b94df9SMatthew Ahrens zfs_handle_t * 504ebedde84SEric Taylor make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc) 505ebedde84SEric Taylor { 506ebedde84SEric Taylor zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 507ebedde84SEric Taylor 508ebedde84SEric Taylor if (zhp == NULL) 509ebedde84SEric Taylor return (NULL); 510ebedde84SEric Taylor 511ebedde84SEric Taylor zhp->zfs_hdl = hdl; 512ebedde84SEric Taylor (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name)); 513ebedde84SEric Taylor if (make_dataset_handle_common(zhp, zc) == -1) { 514ebedde84SEric Taylor free(zhp); 515ebedde84SEric Taylor return (NULL); 516ebedde84SEric Taylor } 517fa9e4066Sahrens return (zhp); 518fa9e4066Sahrens } 519fa9e4066Sahrens 52019b94df9SMatthew Ahrens zfs_handle_t * 52119b94df9SMatthew Ahrens zfs_handle_dup(zfs_handle_t *zhp_orig) 52219b94df9SMatthew Ahrens { 52319b94df9SMatthew Ahrens zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 52419b94df9SMatthew Ahrens 52519b94df9SMatthew Ahrens if (zhp == NULL) 52619b94df9SMatthew Ahrens return (NULL); 52719b94df9SMatthew Ahrens 52819b94df9SMatthew Ahrens zhp->zfs_hdl = zhp_orig->zfs_hdl; 52919b94df9SMatthew Ahrens zhp->zpool_hdl = zhp_orig->zpool_hdl; 53019b94df9SMatthew Ahrens (void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name, 53119b94df9SMatthew Ahrens sizeof (zhp->zfs_name)); 53219b94df9SMatthew Ahrens zhp->zfs_type = zhp_orig->zfs_type; 53319b94df9SMatthew Ahrens zhp->zfs_head_type = zhp_orig->zfs_head_type; 53419b94df9SMatthew Ahrens zhp->zfs_dmustats = zhp_orig->zfs_dmustats; 53519b94df9SMatthew Ahrens if (zhp_orig->zfs_props != NULL) { 53619b94df9SMatthew Ahrens if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) { 53719b94df9SMatthew Ahrens (void) no_memory(zhp->zfs_hdl); 53819b94df9SMatthew Ahrens zfs_close(zhp); 53919b94df9SMatthew Ahrens return (NULL); 54019b94df9SMatthew Ahrens } 54119b94df9SMatthew Ahrens } 54219b94df9SMatthew Ahrens if (zhp_orig->zfs_user_props != NULL) { 54319b94df9SMatthew Ahrens if (nvlist_dup(zhp_orig->zfs_user_props, 54419b94df9SMatthew Ahrens &zhp->zfs_user_props, 0) != 0) { 54519b94df9SMatthew Ahrens (void) no_memory(zhp->zfs_hdl); 54619b94df9SMatthew Ahrens zfs_close(zhp); 54719b94df9SMatthew Ahrens return (NULL); 54819b94df9SMatthew Ahrens } 54919b94df9SMatthew Ahrens } 55019b94df9SMatthew Ahrens if (zhp_orig->zfs_recvd_props != NULL) { 55119b94df9SMatthew Ahrens if (nvlist_dup(zhp_orig->zfs_recvd_props, 55219b94df9SMatthew Ahrens &zhp->zfs_recvd_props, 0)) { 55319b94df9SMatthew Ahrens (void) no_memory(zhp->zfs_hdl); 55419b94df9SMatthew Ahrens zfs_close(zhp); 55519b94df9SMatthew Ahrens return (NULL); 55619b94df9SMatthew Ahrens } 55719b94df9SMatthew Ahrens } 55819b94df9SMatthew Ahrens zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck; 55919b94df9SMatthew Ahrens if (zhp_orig->zfs_mntopts != NULL) { 56019b94df9SMatthew Ahrens zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl, 56119b94df9SMatthew Ahrens zhp_orig->zfs_mntopts); 56219b94df9SMatthew Ahrens } 56319b94df9SMatthew Ahrens zhp->zfs_props_table = zhp_orig->zfs_props_table; 56419b94df9SMatthew Ahrens return (zhp); 56519b94df9SMatthew Ahrens } 56619b94df9SMatthew Ahrens 56778f17100SMatthew Ahrens boolean_t 56878f17100SMatthew Ahrens zfs_bookmark_exists(const char *path) 56978f17100SMatthew Ahrens { 57078f17100SMatthew Ahrens nvlist_t *bmarks; 57178f17100SMatthew Ahrens nvlist_t *props; 57278f17100SMatthew Ahrens char fsname[ZFS_MAXNAMELEN]; 57378f17100SMatthew Ahrens char *bmark_name; 57478f17100SMatthew Ahrens char *pound; 57578f17100SMatthew Ahrens int err; 57678f17100SMatthew Ahrens boolean_t rv; 57778f17100SMatthew Ahrens 57878f17100SMatthew Ahrens 57978f17100SMatthew Ahrens (void) strlcpy(fsname, path, sizeof (fsname)); 58078f17100SMatthew Ahrens pound = strchr(fsname, '#'); 58178f17100SMatthew Ahrens if (pound == NULL) 58278f17100SMatthew Ahrens return (B_FALSE); 58378f17100SMatthew Ahrens 58478f17100SMatthew Ahrens *pound = '\0'; 58578f17100SMatthew Ahrens bmark_name = pound + 1; 58678f17100SMatthew Ahrens props = fnvlist_alloc(); 58778f17100SMatthew Ahrens err = lzc_get_bookmarks(fsname, props, &bmarks); 58878f17100SMatthew Ahrens nvlist_free(props); 58978f17100SMatthew Ahrens if (err != 0) { 59078f17100SMatthew Ahrens nvlist_free(bmarks); 59178f17100SMatthew Ahrens return (B_FALSE); 59278f17100SMatthew Ahrens } 59378f17100SMatthew Ahrens 59478f17100SMatthew Ahrens rv = nvlist_exists(bmarks, bmark_name); 59578f17100SMatthew Ahrens nvlist_free(bmarks); 59678f17100SMatthew Ahrens return (rv); 59778f17100SMatthew Ahrens } 59878f17100SMatthew Ahrens 59978f17100SMatthew Ahrens zfs_handle_t * 60078f17100SMatthew Ahrens make_bookmark_handle(zfs_handle_t *parent, const char *path, 60178f17100SMatthew Ahrens nvlist_t *bmark_props) 60278f17100SMatthew Ahrens { 60378f17100SMatthew Ahrens zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 60478f17100SMatthew Ahrens 60578f17100SMatthew Ahrens if (zhp == NULL) 60678f17100SMatthew Ahrens return (NULL); 60778f17100SMatthew Ahrens 60878f17100SMatthew Ahrens /* Fill in the name. */ 60978f17100SMatthew Ahrens zhp->zfs_hdl = parent->zfs_hdl; 61078f17100SMatthew Ahrens (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name)); 61178f17100SMatthew Ahrens 61278f17100SMatthew Ahrens /* Set the property lists. */ 61378f17100SMatthew Ahrens if (nvlist_dup(bmark_props, &zhp->zfs_props, 0) != 0) { 61478f17100SMatthew Ahrens free(zhp); 61578f17100SMatthew Ahrens return (NULL); 61678f17100SMatthew Ahrens } 61778f17100SMatthew Ahrens 61878f17100SMatthew Ahrens /* Set the types. */ 61978f17100SMatthew Ahrens zhp->zfs_head_type = parent->zfs_head_type; 62078f17100SMatthew Ahrens zhp->zfs_type = ZFS_TYPE_BOOKMARK; 62178f17100SMatthew Ahrens 62278f17100SMatthew Ahrens if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL) { 62378f17100SMatthew Ahrens nvlist_free(zhp->zfs_props); 62478f17100SMatthew Ahrens free(zhp); 62578f17100SMatthew Ahrens return (NULL); 62678f17100SMatthew Ahrens } 62778f17100SMatthew Ahrens 62878f17100SMatthew Ahrens return (zhp); 62978f17100SMatthew Ahrens } 63078f17100SMatthew Ahrens 631fa9e4066Sahrens /* 632fa9e4066Sahrens * Opens the given snapshot, filesystem, or volume. The 'types' 633fa9e4066Sahrens * argument is a mask of acceptable types. The function will print an 634fa9e4066Sahrens * appropriate error message and return NULL if it can't be opened. 635fa9e4066Sahrens */ 636fa9e4066Sahrens zfs_handle_t * 63799653d4eSeschrock zfs_open(libzfs_handle_t *hdl, const char *path, int types) 638fa9e4066Sahrens { 639fa9e4066Sahrens zfs_handle_t *zhp; 64099653d4eSeschrock char errbuf[1024]; 64199653d4eSeschrock 64299653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), 64399653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), path); 644fa9e4066Sahrens 645fa9e4066Sahrens /* 64699653d4eSeschrock * Validate the name before we even try to open it. 647fa9e4066Sahrens */ 648f18faf3fSek110237 if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) { 64999653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 65099653d4eSeschrock "invalid dataset name")); 65199653d4eSeschrock (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 652fa9e4066Sahrens return (NULL); 653fa9e4066Sahrens } 654fa9e4066Sahrens 655fa9e4066Sahrens /* 656fa9e4066Sahrens * Try to get stats for the dataset, which will tell us if it exists. 657fa9e4066Sahrens */ 658fa9e4066Sahrens errno = 0; 65999653d4eSeschrock if ((zhp = make_dataset_handle(hdl, path)) == NULL) { 660ece3d9b3Slling (void) zfs_standard_error(hdl, errno, errbuf); 661fa9e4066Sahrens return (NULL); 662fa9e4066Sahrens } 663fa9e4066Sahrens 664fa9e4066Sahrens if (!(types & zhp->zfs_type)) { 66599653d4eSeschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 66694de1d4cSeschrock zfs_close(zhp); 667fa9e4066Sahrens return (NULL); 668fa9e4066Sahrens } 669fa9e4066Sahrens 670fa9e4066Sahrens return (zhp); 671fa9e4066Sahrens } 672fa9e4066Sahrens 673fa9e4066Sahrens /* 674fa9e4066Sahrens * Release a ZFS handle. Nothing to do but free the associated memory. 675fa9e4066Sahrens */ 676fa9e4066Sahrens void 677fa9e4066Sahrens zfs_close(zfs_handle_t *zhp) 678fa9e4066Sahrens { 679fa9e4066Sahrens if (zhp->zfs_mntopts) 680fa9e4066Sahrens free(zhp->zfs_mntopts); 68199653d4eSeschrock nvlist_free(zhp->zfs_props); 682e9dbad6fSeschrock nvlist_free(zhp->zfs_user_props); 68392241e0bSTom Erickson nvlist_free(zhp->zfs_recvd_props); 684fa9e4066Sahrens free(zhp); 685fa9e4066Sahrens } 686fa9e4066Sahrens 687ebedde84SEric Taylor typedef struct mnttab_node { 688ebedde84SEric Taylor struct mnttab mtn_mt; 689ebedde84SEric Taylor avl_node_t mtn_node; 690ebedde84SEric Taylor } mnttab_node_t; 691ebedde84SEric Taylor 692ebedde84SEric Taylor static int 693ebedde84SEric Taylor libzfs_mnttab_cache_compare(const void *arg1, const void *arg2) 694ebedde84SEric Taylor { 695ebedde84SEric Taylor const mnttab_node_t *mtn1 = arg1; 696ebedde84SEric Taylor const mnttab_node_t *mtn2 = arg2; 697ebedde84SEric Taylor int rv; 698ebedde84SEric Taylor 699ebedde84SEric Taylor rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special); 700ebedde84SEric Taylor 701ebedde84SEric Taylor if (rv == 0) 702ebedde84SEric Taylor return (0); 703ebedde84SEric Taylor return (rv > 0 ? 1 : -1); 704ebedde84SEric Taylor } 705ebedde84SEric Taylor 706ebedde84SEric Taylor void 707ebedde84SEric Taylor libzfs_mnttab_init(libzfs_handle_t *hdl) 708ebedde84SEric Taylor { 709ebedde84SEric Taylor assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0); 710ebedde84SEric Taylor avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare, 711ebedde84SEric Taylor sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node)); 712b2634b9cSEric Taylor } 713b2634b9cSEric Taylor 714b2634b9cSEric Taylor void 715b2634b9cSEric Taylor libzfs_mnttab_update(libzfs_handle_t *hdl) 716b2634b9cSEric Taylor { 717b2634b9cSEric Taylor struct mnttab entry; 718ebedde84SEric Taylor 719ebedde84SEric Taylor rewind(hdl->libzfs_mnttab); 720ebedde84SEric Taylor while (getmntent(hdl->libzfs_mnttab, &entry) == 0) { 721ebedde84SEric Taylor mnttab_node_t *mtn; 722ebedde84SEric Taylor 723ebedde84SEric Taylor if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) 724ebedde84SEric Taylor continue; 725ebedde84SEric Taylor mtn = zfs_alloc(hdl, sizeof (mnttab_node_t)); 726ebedde84SEric Taylor mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special); 727ebedde84SEric Taylor mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp); 728ebedde84SEric Taylor mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype); 729ebedde84SEric Taylor mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts); 730ebedde84SEric Taylor avl_add(&hdl->libzfs_mnttab_cache, mtn); 731ebedde84SEric Taylor } 732ebedde84SEric Taylor } 733ebedde84SEric Taylor 734ebedde84SEric Taylor void 735ebedde84SEric Taylor libzfs_mnttab_fini(libzfs_handle_t *hdl) 736ebedde84SEric Taylor { 737ebedde84SEric Taylor void *cookie = NULL; 738ebedde84SEric Taylor mnttab_node_t *mtn; 739ebedde84SEric Taylor 740ebedde84SEric Taylor while (mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie)) { 741ebedde84SEric Taylor free(mtn->mtn_mt.mnt_special); 742ebedde84SEric Taylor free(mtn->mtn_mt.mnt_mountp); 743ebedde84SEric Taylor free(mtn->mtn_mt.mnt_fstype); 744ebedde84SEric Taylor free(mtn->mtn_mt.mnt_mntopts); 745ebedde84SEric Taylor free(mtn); 746ebedde84SEric Taylor } 747ebedde84SEric Taylor avl_destroy(&hdl->libzfs_mnttab_cache); 748ebedde84SEric Taylor } 749ebedde84SEric Taylor 750b2634b9cSEric Taylor void 751b2634b9cSEric Taylor libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable) 752b2634b9cSEric Taylor { 753b2634b9cSEric Taylor hdl->libzfs_mnttab_enable = enable; 754b2634b9cSEric Taylor } 755b2634b9cSEric Taylor 756ebedde84SEric Taylor int 757ebedde84SEric Taylor libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname, 758ebedde84SEric Taylor struct mnttab *entry) 759ebedde84SEric Taylor { 760ebedde84SEric Taylor mnttab_node_t find; 761ebedde84SEric Taylor mnttab_node_t *mtn; 762ebedde84SEric Taylor 763b2634b9cSEric Taylor if (!hdl->libzfs_mnttab_enable) { 764b2634b9cSEric Taylor struct mnttab srch = { 0 }; 765b2634b9cSEric Taylor 766b2634b9cSEric Taylor if (avl_numnodes(&hdl->libzfs_mnttab_cache)) 767b2634b9cSEric Taylor libzfs_mnttab_fini(hdl); 768b2634b9cSEric Taylor rewind(hdl->libzfs_mnttab); 769b2634b9cSEric Taylor srch.mnt_special = (char *)fsname; 770b2634b9cSEric Taylor srch.mnt_fstype = MNTTYPE_ZFS; 771b2634b9cSEric Taylor if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0) 772b2634b9cSEric Taylor return (0); 773b2634b9cSEric Taylor else 774b2634b9cSEric Taylor return (ENOENT); 775b2634b9cSEric Taylor } 776b2634b9cSEric Taylor 777ebedde84SEric Taylor if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0) 778b2634b9cSEric Taylor libzfs_mnttab_update(hdl); 779ebedde84SEric Taylor 780ebedde84SEric Taylor find.mtn_mt.mnt_special = (char *)fsname; 781ebedde84SEric Taylor mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL); 782ebedde84SEric Taylor if (mtn) { 783ebedde84SEric Taylor *entry = mtn->mtn_mt; 784ebedde84SEric Taylor return (0); 785ebedde84SEric Taylor } 786ebedde84SEric Taylor return (ENOENT); 787ebedde84SEric Taylor } 788ebedde84SEric Taylor 789ebedde84SEric Taylor void 790ebedde84SEric Taylor libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special, 791ebedde84SEric Taylor const char *mountp, const char *mntopts) 792ebedde84SEric Taylor { 793ebedde84SEric Taylor mnttab_node_t *mtn; 794ebedde84SEric Taylor 795ebedde84SEric Taylor if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0) 796ebedde84SEric Taylor return; 797ebedde84SEric Taylor mtn = zfs_alloc(hdl, sizeof (mnttab_node_t)); 798ebedde84SEric Taylor mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special); 799ebedde84SEric Taylor mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp); 800ebedde84SEric Taylor mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS); 801ebedde84SEric Taylor mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts); 802ebedde84SEric Taylor avl_add(&hdl->libzfs_mnttab_cache, mtn); 803ebedde84SEric Taylor } 804ebedde84SEric Taylor 805ebedde84SEric Taylor void 806ebedde84SEric Taylor libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname) 807ebedde84SEric Taylor { 808ebedde84SEric Taylor mnttab_node_t find; 809ebedde84SEric Taylor mnttab_node_t *ret; 810ebedde84SEric Taylor 811ebedde84SEric Taylor find.mtn_mt.mnt_special = (char *)fsname; 812ebedde84SEric Taylor if (ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL)) { 813ebedde84SEric Taylor avl_remove(&hdl->libzfs_mnttab_cache, ret); 814ebedde84SEric Taylor free(ret->mtn_mt.mnt_special); 815ebedde84SEric Taylor free(ret->mtn_mt.mnt_mountp); 816ebedde84SEric Taylor free(ret->mtn_mt.mnt_fstype); 817ebedde84SEric Taylor free(ret->mtn_mt.mnt_mntopts); 818ebedde84SEric Taylor free(ret); 819ebedde84SEric Taylor } 820ebedde84SEric Taylor } 821ebedde84SEric Taylor 8227b97dc1aSrm160521 int 8237b97dc1aSrm160521 zfs_spa_version(zfs_handle_t *zhp, int *spa_version) 8247b97dc1aSrm160521 { 82529ab75c9Srm160521 zpool_handle_t *zpool_handle = zhp->zpool_hdl; 8267b97dc1aSrm160521 8277b97dc1aSrm160521 if (zpool_handle == NULL) 8287b97dc1aSrm160521 return (-1); 8297b97dc1aSrm160521 8307b97dc1aSrm160521 *spa_version = zpool_get_prop_int(zpool_handle, 8317b97dc1aSrm160521 ZPOOL_PROP_VERSION, NULL); 8327b97dc1aSrm160521 return (0); 8337b97dc1aSrm160521 } 8347b97dc1aSrm160521 8357b97dc1aSrm160521 /* 8367b97dc1aSrm160521 * The choice of reservation property depends on the SPA version. 8377b97dc1aSrm160521 */ 8387b97dc1aSrm160521 static int 8397b97dc1aSrm160521 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop) 8407b97dc1aSrm160521 { 8417b97dc1aSrm160521 int spa_version; 8427b97dc1aSrm160521 8437b97dc1aSrm160521 if (zfs_spa_version(zhp, &spa_version) < 0) 8447b97dc1aSrm160521 return (-1); 8457b97dc1aSrm160521 8467b97dc1aSrm160521 if (spa_version >= SPA_VERSION_REFRESERVATION) 8477b97dc1aSrm160521 *resv_prop = ZFS_PROP_REFRESERVATION; 8487b97dc1aSrm160521 else 8497b97dc1aSrm160521 *resv_prop = ZFS_PROP_RESERVATION; 8507b97dc1aSrm160521 8517b97dc1aSrm160521 return (0); 8527b97dc1aSrm160521 } 8537b97dc1aSrm160521 854b1b8ab34Slling /* 855e9dbad6fSeschrock * Given an nvlist of properties to set, validates that they are correct, and 856e9dbad6fSeschrock * parses any numeric properties (index, boolean, etc) if they are specified as 857e9dbad6fSeschrock * strings. 858fa9e4066Sahrens */ 8590a48a24eStimh nvlist_t * 8600a48a24eStimh zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl, 861990b4856Slling uint64_t zoned, zfs_handle_t *zhp, const char *errbuf) 862fa9e4066Sahrens { 863e9dbad6fSeschrock nvpair_t *elem; 864e9dbad6fSeschrock uint64_t intval; 865e9dbad6fSeschrock char *strval; 866990b4856Slling zfs_prop_t prop; 867e9dbad6fSeschrock nvlist_t *ret; 868da6c28aaSamw int chosen_normal = -1; 869da6c28aaSamw int chosen_utf = -1; 870990b4856Slling 871e9dbad6fSeschrock if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) { 872e9dbad6fSeschrock (void) no_memory(hdl); 873e9dbad6fSeschrock return (NULL); 874e9dbad6fSeschrock } 875fa9e4066Sahrens 87614843421SMatthew Ahrens /* 87714843421SMatthew Ahrens * Make sure this property is valid and applies to this type. 87814843421SMatthew Ahrens */ 87914843421SMatthew Ahrens 880e9dbad6fSeschrock elem = NULL; 881e9dbad6fSeschrock while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { 882990b4856Slling const char *propname = nvpair_name(elem); 88399653d4eSeschrock 88414843421SMatthew Ahrens prop = zfs_name_to_prop(propname); 88514843421SMatthew Ahrens if (prop == ZPROP_INVAL && zfs_prop_user(propname)) { 886fa9e4066Sahrens /* 88714843421SMatthew Ahrens * This is a user property: make sure it's a 888990b4856Slling * string, and that it's less than ZAP_MAXNAMELEN. 889990b4856Slling */ 890990b4856Slling if (nvpair_type(elem) != DATA_TYPE_STRING) { 891990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 892990b4856Slling "'%s' must be a string"), propname); 893990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 894990b4856Slling goto error; 895990b4856Slling } 896990b4856Slling 897990b4856Slling if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) { 898e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 899e9dbad6fSeschrock "property name '%s' is too long"), 900e9dbad6fSeschrock propname); 901990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 902e9dbad6fSeschrock goto error; 903e9dbad6fSeschrock } 904e9dbad6fSeschrock 905e9dbad6fSeschrock (void) nvpair_value_string(elem, &strval); 906e9dbad6fSeschrock if (nvlist_add_string(ret, propname, strval) != 0) { 907e9dbad6fSeschrock (void) no_memory(hdl); 908e9dbad6fSeschrock goto error; 909e9dbad6fSeschrock } 910e9dbad6fSeschrock continue; 911e9dbad6fSeschrock } 912fa9e4066Sahrens 91314843421SMatthew Ahrens /* 91414843421SMatthew Ahrens * Currently, only user properties can be modified on 91514843421SMatthew Ahrens * snapshots. 91614843421SMatthew Ahrens */ 917bb0ade09Sahrens if (type == ZFS_TYPE_SNAPSHOT) { 918bb0ade09Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 919bb0ade09Sahrens "this property can not be modified for snapshots")); 920bb0ade09Sahrens (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 921bb0ade09Sahrens goto error; 922bb0ade09Sahrens } 923bb0ade09Sahrens 92414843421SMatthew Ahrens if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) { 92514843421SMatthew Ahrens zfs_userquota_prop_t uqtype; 92614843421SMatthew Ahrens char newpropname[128]; 92714843421SMatthew Ahrens char domain[128]; 92814843421SMatthew Ahrens uint64_t rid; 92914843421SMatthew Ahrens uint64_t valary[3]; 93014843421SMatthew Ahrens 93114843421SMatthew Ahrens if (userquota_propname_decode(propname, zoned, 93214843421SMatthew Ahrens &uqtype, domain, sizeof (domain), &rid) != 0) { 93314843421SMatthew Ahrens zfs_error_aux(hdl, 93414843421SMatthew Ahrens dgettext(TEXT_DOMAIN, 93514843421SMatthew Ahrens "'%s' has an invalid user/group name"), 93614843421SMatthew Ahrens propname); 93714843421SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 93814843421SMatthew Ahrens goto error; 93914843421SMatthew Ahrens } 94014843421SMatthew Ahrens 94114843421SMatthew Ahrens if (uqtype != ZFS_PROP_USERQUOTA && 94214843421SMatthew Ahrens uqtype != ZFS_PROP_GROUPQUOTA) { 94314843421SMatthew Ahrens zfs_error_aux(hdl, 94414843421SMatthew Ahrens dgettext(TEXT_DOMAIN, "'%s' is readonly"), 94514843421SMatthew Ahrens propname); 94614843421SMatthew Ahrens (void) zfs_error(hdl, EZFS_PROPREADONLY, 94714843421SMatthew Ahrens errbuf); 94814843421SMatthew Ahrens goto error; 94914843421SMatthew Ahrens } 95014843421SMatthew Ahrens 95114843421SMatthew Ahrens if (nvpair_type(elem) == DATA_TYPE_STRING) { 95214843421SMatthew Ahrens (void) nvpair_value_string(elem, &strval); 95314843421SMatthew Ahrens if (strcmp(strval, "none") == 0) { 95414843421SMatthew Ahrens intval = 0; 95514843421SMatthew Ahrens } else if (zfs_nicestrtonum(hdl, 95614843421SMatthew Ahrens strval, &intval) != 0) { 95714843421SMatthew Ahrens (void) zfs_error(hdl, 95814843421SMatthew Ahrens EZFS_BADPROP, errbuf); 95914843421SMatthew Ahrens goto error; 96014843421SMatthew Ahrens } 96114843421SMatthew Ahrens } else if (nvpair_type(elem) == 96214843421SMatthew Ahrens DATA_TYPE_UINT64) { 96314843421SMatthew Ahrens (void) nvpair_value_uint64(elem, &intval); 96414843421SMatthew Ahrens if (intval == 0) { 96514843421SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 96614843421SMatthew Ahrens "use 'none' to disable " 96714843421SMatthew Ahrens "userquota/groupquota")); 96814843421SMatthew Ahrens goto error; 96914843421SMatthew Ahrens } 97014843421SMatthew Ahrens } else { 97114843421SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 97214843421SMatthew Ahrens "'%s' must be a number"), propname); 97314843421SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 97414843421SMatthew Ahrens goto error; 97514843421SMatthew Ahrens } 97614843421SMatthew Ahrens 9772d5843dbSMatthew Ahrens /* 9782d5843dbSMatthew Ahrens * Encode the prop name as 9792d5843dbSMatthew Ahrens * userquota@<hex-rid>-domain, to make it easy 9802d5843dbSMatthew Ahrens * for the kernel to decode. 9812d5843dbSMatthew Ahrens */ 98214843421SMatthew Ahrens (void) snprintf(newpropname, sizeof (newpropname), 9832d5843dbSMatthew Ahrens "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype], 9842d5843dbSMatthew Ahrens (longlong_t)rid, domain); 98514843421SMatthew Ahrens valary[0] = uqtype; 98614843421SMatthew Ahrens valary[1] = rid; 98714843421SMatthew Ahrens valary[2] = intval; 98814843421SMatthew Ahrens if (nvlist_add_uint64_array(ret, newpropname, 98914843421SMatthew Ahrens valary, 3) != 0) { 99014843421SMatthew Ahrens (void) no_memory(hdl); 99114843421SMatthew Ahrens goto error; 99214843421SMatthew Ahrens } 99314843421SMatthew Ahrens continue; 99419b94df9SMatthew Ahrens } else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) { 99519b94df9SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 99619b94df9SMatthew Ahrens "'%s' is readonly"), 99719b94df9SMatthew Ahrens propname); 99819b94df9SMatthew Ahrens (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 99919b94df9SMatthew Ahrens goto error; 100014843421SMatthew Ahrens } 100114843421SMatthew Ahrens 100214843421SMatthew Ahrens if (prop == ZPROP_INVAL) { 100314843421SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 100414843421SMatthew Ahrens "invalid property '%s'"), propname); 100514843421SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 100614843421SMatthew Ahrens goto error; 100714843421SMatthew Ahrens } 100814843421SMatthew Ahrens 1009e9dbad6fSeschrock if (!zfs_prop_valid_for_type(prop, type)) { 1010e9dbad6fSeschrock zfs_error_aux(hdl, 1011e9dbad6fSeschrock dgettext(TEXT_DOMAIN, "'%s' does not " 1012e9dbad6fSeschrock "apply to datasets of this type"), propname); 1013e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 1014e9dbad6fSeschrock goto error; 1015e9dbad6fSeschrock } 1016e9dbad6fSeschrock 1017e9dbad6fSeschrock if (zfs_prop_readonly(prop) && 1018da6c28aaSamw (!zfs_prop_setonce(prop) || zhp != NULL)) { 1019e9dbad6fSeschrock zfs_error_aux(hdl, 1020e9dbad6fSeschrock dgettext(TEXT_DOMAIN, "'%s' is readonly"), 1021e9dbad6fSeschrock propname); 1022e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 1023e9dbad6fSeschrock goto error; 1024e9dbad6fSeschrock } 1025e9dbad6fSeschrock 1026990b4856Slling if (zprop_parse_value(hdl, elem, prop, type, ret, 1027990b4856Slling &strval, &intval, errbuf) != 0) 1028e9dbad6fSeschrock goto error; 1029e9dbad6fSeschrock 1030e9dbad6fSeschrock /* 1031e9dbad6fSeschrock * Perform some additional checks for specific properties. 1032e9dbad6fSeschrock */ 1033e9dbad6fSeschrock switch (prop) { 1034e7437265Sahrens case ZFS_PROP_VERSION: 1035e7437265Sahrens { 1036e7437265Sahrens int version; 1037e7437265Sahrens 1038e7437265Sahrens if (zhp == NULL) 1039e7437265Sahrens break; 1040e7437265Sahrens version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION); 1041e7437265Sahrens if (intval < version) { 1042e7437265Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1043e7437265Sahrens "Can not downgrade; already at version %u"), 1044e7437265Sahrens version); 1045e7437265Sahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1046e7437265Sahrens goto error; 1047e7437265Sahrens } 1048e7437265Sahrens break; 1049e7437265Sahrens } 1050e7437265Sahrens 1051e9dbad6fSeschrock case ZFS_PROP_RECORDSIZE: 1052e9dbad6fSeschrock case ZFS_PROP_VOLBLOCKSIZE: 1053e9dbad6fSeschrock /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */ 1054e9dbad6fSeschrock if (intval < SPA_MINBLOCKSIZE || 1055e9dbad6fSeschrock intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) { 1056e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1057e9dbad6fSeschrock "'%s' must be power of 2 from %u " 1058e9dbad6fSeschrock "to %uk"), propname, 1059e9dbad6fSeschrock (uint_t)SPA_MINBLOCKSIZE, 1060e9dbad6fSeschrock (uint_t)SPA_MAXBLOCKSIZE >> 10); 1061e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1062e9dbad6fSeschrock goto error; 1063e9dbad6fSeschrock } 1064e9dbad6fSeschrock break; 1065e9dbad6fSeschrock 10664201a95eSRic Aleshire case ZFS_PROP_MLSLABEL: 10674201a95eSRic Aleshire { 10684201a95eSRic Aleshire /* 10694201a95eSRic Aleshire * Verify the mlslabel string and convert to 10704201a95eSRic Aleshire * internal hex label string. 10714201a95eSRic Aleshire */ 10724201a95eSRic Aleshire 10734201a95eSRic Aleshire m_label_t *new_sl; 10744201a95eSRic Aleshire char *hex = NULL; /* internal label string */ 10754201a95eSRic Aleshire 10764201a95eSRic Aleshire /* Default value is already OK. */ 10774201a95eSRic Aleshire if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0) 10784201a95eSRic Aleshire break; 10794201a95eSRic Aleshire 10804201a95eSRic Aleshire /* Verify the label can be converted to binary form */ 10814201a95eSRic Aleshire if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) || 10824201a95eSRic Aleshire (str_to_label(strval, &new_sl, MAC_LABEL, 10834201a95eSRic Aleshire L_NO_CORRECTION, NULL) == -1)) { 10844201a95eSRic Aleshire goto badlabel; 10854201a95eSRic Aleshire } 10864201a95eSRic Aleshire 10874201a95eSRic Aleshire /* Now translate to hex internal label string */ 10884201a95eSRic Aleshire if (label_to_str(new_sl, &hex, M_INTERNAL, 10894201a95eSRic Aleshire DEF_NAMES) != 0) { 10904201a95eSRic Aleshire if (hex) 10914201a95eSRic Aleshire free(hex); 10924201a95eSRic Aleshire goto badlabel; 10934201a95eSRic Aleshire } 10944201a95eSRic Aleshire m_label_free(new_sl); 10954201a95eSRic Aleshire 10964201a95eSRic Aleshire /* If string is already in internal form, we're done. */ 10974201a95eSRic Aleshire if (strcmp(strval, hex) == 0) { 10984201a95eSRic Aleshire free(hex); 10994201a95eSRic Aleshire break; 11004201a95eSRic Aleshire } 11014201a95eSRic Aleshire 11024201a95eSRic Aleshire /* Replace the label string with the internal form. */ 1103569038c9SRic Aleshire (void) nvlist_remove(ret, zfs_prop_to_name(prop), 11044201a95eSRic Aleshire DATA_TYPE_STRING); 11054201a95eSRic Aleshire verify(nvlist_add_string(ret, zfs_prop_to_name(prop), 11064201a95eSRic Aleshire hex) == 0); 11074201a95eSRic Aleshire free(hex); 11084201a95eSRic Aleshire 11094201a95eSRic Aleshire break; 11104201a95eSRic Aleshire 11114201a95eSRic Aleshire badlabel: 11124201a95eSRic Aleshire zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11134201a95eSRic Aleshire "invalid mlslabel '%s'"), strval); 11144201a95eSRic Aleshire (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 11154201a95eSRic Aleshire m_label_free(new_sl); /* OK if null */ 11164201a95eSRic Aleshire goto error; 11174201a95eSRic Aleshire 11184201a95eSRic Aleshire } 11194201a95eSRic Aleshire 1120e9dbad6fSeschrock case ZFS_PROP_MOUNTPOINT: 112189eef05eSrm160521 { 112289eef05eSrm160521 namecheck_err_t why; 112389eef05eSrm160521 1124e9dbad6fSeschrock if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 || 1125e9dbad6fSeschrock strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0) 1126e9dbad6fSeschrock break; 1127e9dbad6fSeschrock 112889eef05eSrm160521 if (mountpoint_namecheck(strval, &why)) { 112989eef05eSrm160521 switch (why) { 113089eef05eSrm160521 case NAME_ERR_LEADING_SLASH: 113189eef05eSrm160521 zfs_error_aux(hdl, 113289eef05eSrm160521 dgettext(TEXT_DOMAIN, 1133e9dbad6fSeschrock "'%s' must be an absolute path, " 1134e9dbad6fSeschrock "'none', or 'legacy'"), propname); 113589eef05eSrm160521 break; 113689eef05eSrm160521 case NAME_ERR_TOOLONG: 113789eef05eSrm160521 zfs_error_aux(hdl, 113889eef05eSrm160521 dgettext(TEXT_DOMAIN, 113989eef05eSrm160521 "component of '%s' is too long"), 114089eef05eSrm160521 propname); 114189eef05eSrm160521 break; 114289eef05eSrm160521 } 1143e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1144e9dbad6fSeschrock goto error; 1145e9dbad6fSeschrock } 114689eef05eSrm160521 } 114789eef05eSrm160521 1148f3861e1aSahl /*FALLTHRU*/ 1149e9dbad6fSeschrock 1150da6c28aaSamw case ZFS_PROP_SHARESMB: 1151f3861e1aSahl case ZFS_PROP_SHARENFS: 1152e9dbad6fSeschrock /* 1153da6c28aaSamw * For the mountpoint and sharenfs or sharesmb 1154da6c28aaSamw * properties, check if it can be set in a 1155da6c28aaSamw * global/non-global zone based on 1156f3861e1aSahl * the zoned property value: 1157fa9e4066Sahrens * 1158fa9e4066Sahrens * global zone non-global zone 1159f3861e1aSahl * -------------------------------------------------- 1160fa9e4066Sahrens * zoned=on mountpoint (no) mountpoint (yes) 1161fa9e4066Sahrens * sharenfs (no) sharenfs (no) 1162da6c28aaSamw * sharesmb (no) sharesmb (no) 1163fa9e4066Sahrens * 1164fa9e4066Sahrens * zoned=off mountpoint (yes) N/A 1165fa9e4066Sahrens * sharenfs (yes) 1166da6c28aaSamw * sharesmb (yes) 1167fa9e4066Sahrens */ 1168e9dbad6fSeschrock if (zoned) { 1169fa9e4066Sahrens if (getzoneid() == GLOBAL_ZONEID) { 117099653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1171e9dbad6fSeschrock "'%s' cannot be set on " 1172e9dbad6fSeschrock "dataset in a non-global zone"), 1173e9dbad6fSeschrock propname); 1174e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_ZONED, 1175e9dbad6fSeschrock errbuf); 1176e9dbad6fSeschrock goto error; 1177da6c28aaSamw } else if (prop == ZFS_PROP_SHARENFS || 1178da6c28aaSamw prop == ZFS_PROP_SHARESMB) { 117999653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1180e9dbad6fSeschrock "'%s' cannot be set in " 1181e9dbad6fSeschrock "a non-global zone"), propname); 1182e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_ZONED, 1183e9dbad6fSeschrock errbuf); 1184e9dbad6fSeschrock goto error; 1185fa9e4066Sahrens } 1186fa9e4066Sahrens } else if (getzoneid() != GLOBAL_ZONEID) { 1187fa9e4066Sahrens /* 1188fa9e4066Sahrens * If zoned property is 'off', this must be in 118914843421SMatthew Ahrens * a global zone. If not, something is wrong. 1190fa9e4066Sahrens */ 119199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1192e9dbad6fSeschrock "'%s' cannot be set while dataset " 1193e9dbad6fSeschrock "'zoned' property is set"), propname); 1194e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_ZONED, errbuf); 1195e9dbad6fSeschrock goto error; 1196fa9e4066Sahrens } 1197f3861e1aSahl 119867331909Sdougm /* 119967331909Sdougm * At this point, it is legitimate to set the 120067331909Sdougm * property. Now we want to make sure that the 120167331909Sdougm * property value is valid if it is sharenfs. 120267331909Sdougm */ 1203da6c28aaSamw if ((prop == ZFS_PROP_SHARENFS || 1204da6c28aaSamw prop == ZFS_PROP_SHARESMB) && 120567331909Sdougm strcmp(strval, "on") != 0 && 120667331909Sdougm strcmp(strval, "off") != 0) { 1207da6c28aaSamw zfs_share_proto_t proto; 1208da6c28aaSamw 1209da6c28aaSamw if (prop == ZFS_PROP_SHARESMB) 1210da6c28aaSamw proto = PROTO_SMB; 1211da6c28aaSamw else 1212da6c28aaSamw proto = PROTO_NFS; 121367331909Sdougm 121467331909Sdougm /* 1215da6c28aaSamw * Must be an valid sharing protocol 1216da6c28aaSamw * option string so init the libshare 1217da6c28aaSamw * in order to enable the parser and 1218da6c28aaSamw * then parse the options. We use the 1219da6c28aaSamw * control API since we don't care about 1220da6c28aaSamw * the current configuration and don't 122167331909Sdougm * want the overhead of loading it 122267331909Sdougm * until we actually do something. 122367331909Sdougm */ 122467331909Sdougm 122567331909Sdougm if (zfs_init_libshare(hdl, 122667331909Sdougm SA_INIT_CONTROL_API) != SA_OK) { 1227fac3008cSeschrock /* 1228fac3008cSeschrock * An error occurred so we can't do 1229fac3008cSeschrock * anything 1230fac3008cSeschrock */ 123167331909Sdougm zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 123267331909Sdougm "'%s' cannot be set: problem " 123367331909Sdougm "in share initialization"), 123467331909Sdougm propname); 1235fac3008cSeschrock (void) zfs_error(hdl, EZFS_BADPROP, 1236fac3008cSeschrock errbuf); 123767331909Sdougm goto error; 123867331909Sdougm } 123967331909Sdougm 1240da6c28aaSamw if (zfs_parse_options(strval, proto) != SA_OK) { 124167331909Sdougm /* 124267331909Sdougm * There was an error in parsing so 124367331909Sdougm * deal with it by issuing an error 124467331909Sdougm * message and leaving after 124567331909Sdougm * uninitializing the the libshare 124667331909Sdougm * interface. 124767331909Sdougm */ 124867331909Sdougm zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 124967331909Sdougm "'%s' cannot be set to invalid " 125067331909Sdougm "options"), propname); 1251fac3008cSeschrock (void) zfs_error(hdl, EZFS_BADPROP, 1252fac3008cSeschrock errbuf); 125367331909Sdougm zfs_uninit_libshare(hdl); 125467331909Sdougm goto error; 125567331909Sdougm } 125667331909Sdougm zfs_uninit_libshare(hdl); 125767331909Sdougm } 125867331909Sdougm 1259f3861e1aSahl break; 1260da6c28aaSamw case ZFS_PROP_UTF8ONLY: 1261da6c28aaSamw chosen_utf = (int)intval; 1262da6c28aaSamw break; 1263da6c28aaSamw case ZFS_PROP_NORMALIZE: 1264da6c28aaSamw chosen_normal = (int)intval; 1265da6c28aaSamw break; 1266fa9e4066Sahrens } 1267fa9e4066Sahrens 1268e9dbad6fSeschrock /* 1269e9dbad6fSeschrock * For changes to existing volumes, we have some additional 1270e9dbad6fSeschrock * checks to enforce. 1271e9dbad6fSeschrock */ 1272e9dbad6fSeschrock if (type == ZFS_TYPE_VOLUME && zhp != NULL) { 1273e9dbad6fSeschrock uint64_t volsize = zfs_prop_get_int(zhp, 1274e9dbad6fSeschrock ZFS_PROP_VOLSIZE); 1275e9dbad6fSeschrock uint64_t blocksize = zfs_prop_get_int(zhp, 1276e9dbad6fSeschrock ZFS_PROP_VOLBLOCKSIZE); 1277e9dbad6fSeschrock char buf[64]; 1278e9dbad6fSeschrock 1279e9dbad6fSeschrock switch (prop) { 1280e9dbad6fSeschrock case ZFS_PROP_RESERVATION: 1281a9799022Sck153898 case ZFS_PROP_REFRESERVATION: 1282e9dbad6fSeschrock if (intval > volsize) { 1283e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1284e9dbad6fSeschrock "'%s' is greater than current " 1285e9dbad6fSeschrock "volume size"), propname); 1286e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_BADPROP, 1287e9dbad6fSeschrock errbuf); 1288e9dbad6fSeschrock goto error; 1289e9dbad6fSeschrock } 1290e9dbad6fSeschrock break; 1291e9dbad6fSeschrock 1292e9dbad6fSeschrock case ZFS_PROP_VOLSIZE: 1293e9dbad6fSeschrock if (intval % blocksize != 0) { 1294e9dbad6fSeschrock zfs_nicenum(blocksize, buf, 1295e9dbad6fSeschrock sizeof (buf)); 1296e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1297e9dbad6fSeschrock "'%s' must be a multiple of " 1298e9dbad6fSeschrock "volume block size (%s)"), 1299e9dbad6fSeschrock propname, buf); 1300e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_BADPROP, 1301e9dbad6fSeschrock errbuf); 1302e9dbad6fSeschrock goto error; 1303e9dbad6fSeschrock } 1304e9dbad6fSeschrock 1305e9dbad6fSeschrock if (intval == 0) { 1306e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1307e9dbad6fSeschrock "'%s' cannot be zero"), 1308e9dbad6fSeschrock propname); 1309e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_BADPROP, 1310e9dbad6fSeschrock errbuf); 1311e9dbad6fSeschrock goto error; 1312e9dbad6fSeschrock } 1313f3861e1aSahl break; 1314e9dbad6fSeschrock } 1315e9dbad6fSeschrock } 1316e9dbad6fSeschrock } 1317e9dbad6fSeschrock 1318e9dbad6fSeschrock /* 1319da6c28aaSamw * If normalization was chosen, but no UTF8 choice was made, 1320da6c28aaSamw * enforce rejection of non-UTF8 names. 1321da6c28aaSamw * 1322da6c28aaSamw * If normalization was chosen, but rejecting non-UTF8 names 1323da6c28aaSamw * was explicitly not chosen, it is an error. 1324da6c28aaSamw */ 1325de8267e0Stimh if (chosen_normal > 0 && chosen_utf < 0) { 1326da6c28aaSamw if (nvlist_add_uint64(ret, 1327da6c28aaSamw zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) { 1328da6c28aaSamw (void) no_memory(hdl); 1329da6c28aaSamw goto error; 1330da6c28aaSamw } 1331de8267e0Stimh } else if (chosen_normal > 0 && chosen_utf == 0) { 1332da6c28aaSamw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1333da6c28aaSamw "'%s' must be set 'on' if normalization chosen"), 1334da6c28aaSamw zfs_prop_to_name(ZFS_PROP_UTF8ONLY)); 1335da6c28aaSamw (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1336da6c28aaSamw goto error; 1337da6c28aaSamw } 1338e9dbad6fSeschrock return (ret); 1339e9dbad6fSeschrock 1340e9dbad6fSeschrock error: 1341e9dbad6fSeschrock nvlist_free(ret); 1342e9dbad6fSeschrock return (NULL); 1343e9dbad6fSeschrock } 1344e9dbad6fSeschrock 134536db6475SEric Taylor int 134636db6475SEric Taylor zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl) 134736db6475SEric Taylor { 134836db6475SEric Taylor uint64_t old_volsize; 134936db6475SEric Taylor uint64_t new_volsize; 135036db6475SEric Taylor uint64_t old_reservation; 135136db6475SEric Taylor uint64_t new_reservation; 135236db6475SEric Taylor zfs_prop_t resv_prop; 1353c61ea566SGeorge Wilson nvlist_t *props; 135436db6475SEric Taylor 135536db6475SEric Taylor /* 135636db6475SEric Taylor * If this is an existing volume, and someone is setting the volsize, 135736db6475SEric Taylor * make sure that it matches the reservation, or add it if necessary. 135836db6475SEric Taylor */ 135936db6475SEric Taylor old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 136036db6475SEric Taylor if (zfs_which_resv_prop(zhp, &resv_prop) < 0) 136136db6475SEric Taylor return (-1); 136236db6475SEric Taylor old_reservation = zfs_prop_get_int(zhp, resv_prop); 1363c61ea566SGeorge Wilson 1364c61ea566SGeorge Wilson props = fnvlist_alloc(); 1365c61ea566SGeorge Wilson fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 1366c61ea566SGeorge Wilson zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE)); 1367c61ea566SGeorge Wilson 1368c61ea566SGeorge Wilson if ((zvol_volsize_to_reservation(old_volsize, props) != 1369c61ea566SGeorge Wilson old_reservation) || nvlist_exists(nvl, 1370c61ea566SGeorge Wilson zfs_prop_to_name(resv_prop))) { 1371c61ea566SGeorge Wilson fnvlist_free(props); 137236db6475SEric Taylor return (0); 137336db6475SEric Taylor } 137436db6475SEric Taylor if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE), 1375c61ea566SGeorge Wilson &new_volsize) != 0) { 1376c61ea566SGeorge Wilson fnvlist_free(props); 137736db6475SEric Taylor return (-1); 1378c61ea566SGeorge Wilson } 1379c61ea566SGeorge Wilson new_reservation = zvol_volsize_to_reservation(new_volsize, props); 1380c61ea566SGeorge Wilson fnvlist_free(props); 1381c61ea566SGeorge Wilson 138236db6475SEric Taylor if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop), 138336db6475SEric Taylor new_reservation) != 0) { 138436db6475SEric Taylor (void) no_memory(zhp->zfs_hdl); 138536db6475SEric Taylor return (-1); 138636db6475SEric Taylor } 138736db6475SEric Taylor return (1); 138836db6475SEric Taylor } 138936db6475SEric Taylor 139092241e0bSTom Erickson void 139192241e0bSTom Erickson zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err, 139292241e0bSTom Erickson char *errbuf) 139392241e0bSTom Erickson { 139492241e0bSTom Erickson switch (err) { 139592241e0bSTom Erickson 139692241e0bSTom Erickson case ENOSPC: 139792241e0bSTom Erickson /* 139892241e0bSTom Erickson * For quotas and reservations, ENOSPC indicates 139992241e0bSTom Erickson * something different; setting a quota or reservation 140092241e0bSTom Erickson * doesn't use any disk space. 140192241e0bSTom Erickson */ 140292241e0bSTom Erickson switch (prop) { 140392241e0bSTom Erickson case ZFS_PROP_QUOTA: 140492241e0bSTom Erickson case ZFS_PROP_REFQUOTA: 140592241e0bSTom Erickson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 140692241e0bSTom Erickson "size is less than current used or " 140792241e0bSTom Erickson "reserved space")); 140892241e0bSTom Erickson (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 140992241e0bSTom Erickson break; 141092241e0bSTom Erickson 141192241e0bSTom Erickson case ZFS_PROP_RESERVATION: 141292241e0bSTom Erickson case ZFS_PROP_REFRESERVATION: 141392241e0bSTom Erickson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 141492241e0bSTom Erickson "size is greater than available space")); 141592241e0bSTom Erickson (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 141692241e0bSTom Erickson break; 141792241e0bSTom Erickson 141892241e0bSTom Erickson default: 141992241e0bSTom Erickson (void) zfs_standard_error(hdl, err, errbuf); 142092241e0bSTom Erickson break; 142192241e0bSTom Erickson } 142292241e0bSTom Erickson break; 142392241e0bSTom Erickson 142492241e0bSTom Erickson case EBUSY: 142592241e0bSTom Erickson (void) zfs_standard_error(hdl, EBUSY, errbuf); 142692241e0bSTom Erickson break; 142792241e0bSTom Erickson 142892241e0bSTom Erickson case EROFS: 142992241e0bSTom Erickson (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf); 143092241e0bSTom Erickson break; 143192241e0bSTom Erickson 143292241e0bSTom Erickson case ENOTSUP: 143392241e0bSTom Erickson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 143492241e0bSTom Erickson "pool and or dataset must be upgraded to set this " 143592241e0bSTom Erickson "property or value")); 143692241e0bSTom Erickson (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 143792241e0bSTom Erickson break; 143892241e0bSTom Erickson 143992241e0bSTom Erickson case ERANGE: 144092241e0bSTom Erickson if (prop == ZFS_PROP_COMPRESSION) { 144192241e0bSTom Erickson (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 144292241e0bSTom Erickson "property setting is not allowed on " 144392241e0bSTom Erickson "bootable datasets")); 144492241e0bSTom Erickson (void) zfs_error(hdl, EZFS_NOTSUP, errbuf); 144592241e0bSTom Erickson } else { 144692241e0bSTom Erickson (void) zfs_standard_error(hdl, err, errbuf); 144792241e0bSTom Erickson } 144892241e0bSTom Erickson break; 144992241e0bSTom Erickson 1450ab003da8SJim Dunham case EINVAL: 1451ab003da8SJim Dunham if (prop == ZPROP_INVAL) { 1452ab003da8SJim Dunham (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1453ab003da8SJim Dunham } else { 1454ab003da8SJim Dunham (void) zfs_standard_error(hdl, err, errbuf); 1455ab003da8SJim Dunham } 1456ab003da8SJim Dunham break; 1457ab003da8SJim Dunham 145892241e0bSTom Erickson case EOVERFLOW: 145992241e0bSTom Erickson /* 146092241e0bSTom Erickson * This platform can't address a volume this big. 146192241e0bSTom Erickson */ 146292241e0bSTom Erickson #ifdef _ILP32 146392241e0bSTom Erickson if (prop == ZFS_PROP_VOLSIZE) { 146492241e0bSTom Erickson (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf); 146592241e0bSTom Erickson break; 146692241e0bSTom Erickson } 146792241e0bSTom Erickson #endif 146892241e0bSTom Erickson /* FALLTHROUGH */ 146992241e0bSTom Erickson default: 147092241e0bSTom Erickson (void) zfs_standard_error(hdl, err, errbuf); 147192241e0bSTom Erickson } 147292241e0bSTom Erickson } 147392241e0bSTom Erickson 1474e9dbad6fSeschrock /* 1475e9dbad6fSeschrock * Given a property name and value, set the property for the given dataset. 1476e9dbad6fSeschrock */ 1477e9dbad6fSeschrock int 1478e9dbad6fSeschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval) 1479e9dbad6fSeschrock { 1480e9dbad6fSeschrock zfs_cmd_t zc = { 0 }; 1481e9dbad6fSeschrock int ret = -1; 1482e9dbad6fSeschrock prop_changelist_t *cl = NULL; 1483e9dbad6fSeschrock char errbuf[1024]; 1484e9dbad6fSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 1485e9dbad6fSeschrock nvlist_t *nvl = NULL, *realprops; 1486e9dbad6fSeschrock zfs_prop_t prop; 14874445fffbSMatthew Ahrens boolean_t do_prefix = B_TRUE; 148836db6475SEric Taylor int added_resv; 1489e9dbad6fSeschrock 1490e9dbad6fSeschrock (void) snprintf(errbuf, sizeof (errbuf), 1491e9dbad6fSeschrock dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 1492e9dbad6fSeschrock zhp->zfs_name); 1493e9dbad6fSeschrock 1494e9dbad6fSeschrock if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 || 1495e9dbad6fSeschrock nvlist_add_string(nvl, propname, propval) != 0) { 1496e9dbad6fSeschrock (void) no_memory(hdl); 1497e9dbad6fSeschrock goto error; 1498e9dbad6fSeschrock } 1499e9dbad6fSeschrock 15000a48a24eStimh if ((realprops = zfs_valid_proplist(hdl, zhp->zfs_type, nvl, 1501e9dbad6fSeschrock zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL) 1502e9dbad6fSeschrock goto error; 1503990b4856Slling 1504e9dbad6fSeschrock nvlist_free(nvl); 1505e9dbad6fSeschrock nvl = realprops; 1506e9dbad6fSeschrock 1507e9dbad6fSeschrock prop = zfs_name_to_prop(propname); 1508e9dbad6fSeschrock 150936db6475SEric Taylor if (prop == ZFS_PROP_VOLSIZE) { 151036db6475SEric Taylor if ((added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1) 151136db6475SEric Taylor goto error; 151236db6475SEric Taylor } 151336db6475SEric Taylor 15140069fd67STim Haley if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL) 1515e9dbad6fSeschrock goto error; 1516fa9e4066Sahrens 1517fa9e4066Sahrens if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 151899653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1519fa9e4066Sahrens "child dataset with inherited mountpoint is used " 152099653d4eSeschrock "in a non-global zone")); 152199653d4eSeschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1522fa9e4066Sahrens goto error; 1523fa9e4066Sahrens } 1524fa9e4066Sahrens 15250068372bSMark J Musante /* 15264445fffbSMatthew Ahrens * We don't want to unmount & remount the dataset when changing 15274445fffbSMatthew Ahrens * its canmount property to 'on' or 'noauto'. We only use 15284445fffbSMatthew Ahrens * the changelist logic to unmount when setting canmount=off. 15290068372bSMark J Musante */ 15304445fffbSMatthew Ahrens if (prop == ZFS_PROP_CANMOUNT) { 15314445fffbSMatthew Ahrens uint64_t idx; 15324445fffbSMatthew Ahrens int err = zprop_string_to_index(prop, propval, &idx, 15334445fffbSMatthew Ahrens ZFS_TYPE_DATASET); 15344445fffbSMatthew Ahrens if (err == 0 && idx != ZFS_CANMOUNT_OFF) 15354445fffbSMatthew Ahrens do_prefix = B_FALSE; 15364445fffbSMatthew Ahrens } 1537a227b7f4Shs24103 1538a227b7f4Shs24103 if (do_prefix && (ret = changelist_prefix(cl)) != 0) 1539fa9e4066Sahrens goto error; 1540fa9e4066Sahrens 1541fa9e4066Sahrens /* 1542fa9e4066Sahrens * Execute the corresponding ioctl() to set this property. 1543fa9e4066Sahrens */ 1544fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1545fa9e4066Sahrens 1546990b4856Slling if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0) 1547e9dbad6fSeschrock goto error; 1548e9dbad6fSeschrock 1549ecd6cf80Smarks ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc); 1550743a77edSAlan Wright 1551fa9e4066Sahrens if (ret != 0) { 155292241e0bSTom Erickson zfs_setprop_error(hdl, prop, errno, errbuf); 155336db6475SEric Taylor if (added_resv && errno == ENOSPC) { 155436db6475SEric Taylor /* clean up the volsize property we tried to set */ 155536db6475SEric Taylor uint64_t old_volsize = zfs_prop_get_int(zhp, 155636db6475SEric Taylor ZFS_PROP_VOLSIZE); 155736db6475SEric Taylor nvlist_free(nvl); 155836db6475SEric Taylor zcmd_free_nvlists(&zc); 155936db6475SEric Taylor if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) 156036db6475SEric Taylor goto error; 156136db6475SEric Taylor if (nvlist_add_uint64(nvl, 156236db6475SEric Taylor zfs_prop_to_name(ZFS_PROP_VOLSIZE), 156336db6475SEric Taylor old_volsize) != 0) 156436db6475SEric Taylor goto error; 156536db6475SEric Taylor if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0) 156636db6475SEric Taylor goto error; 156736db6475SEric Taylor (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc); 156836db6475SEric Taylor } 1569fa9e4066Sahrens } else { 1570a227b7f4Shs24103 if (do_prefix) 1571a227b7f4Shs24103 ret = changelist_postfix(cl); 1572a227b7f4Shs24103 1573fa9e4066Sahrens /* 1574fa9e4066Sahrens * Refresh the statistics so the new property value 1575fa9e4066Sahrens * is reflected. 1576fa9e4066Sahrens */ 1577a227b7f4Shs24103 if (ret == 0) 1578fa9e4066Sahrens (void) get_stats(zhp); 1579fa9e4066Sahrens } 1580fa9e4066Sahrens 1581fa9e4066Sahrens error: 1582e9dbad6fSeschrock nvlist_free(nvl); 1583e9dbad6fSeschrock zcmd_free_nvlists(&zc); 1584e9dbad6fSeschrock if (cl) 1585fa9e4066Sahrens changelist_free(cl); 1586fa9e4066Sahrens return (ret); 1587fa9e4066Sahrens } 1588fa9e4066Sahrens 1589fa9e4066Sahrens /* 159092241e0bSTom Erickson * Given a property, inherit the value from the parent dataset, or if received 159192241e0bSTom Erickson * is TRUE, revert to the received value, if any. 1592fa9e4066Sahrens */ 1593fa9e4066Sahrens int 159492241e0bSTom Erickson zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received) 1595fa9e4066Sahrens { 1596fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 1597fa9e4066Sahrens int ret; 1598fa9e4066Sahrens prop_changelist_t *cl; 159999653d4eSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 160099653d4eSeschrock char errbuf[1024]; 1601e9dbad6fSeschrock zfs_prop_t prop; 160299653d4eSeschrock 160399653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 160499653d4eSeschrock "cannot inherit %s for '%s'"), propname, zhp->zfs_name); 1605fa9e4066Sahrens 160692241e0bSTom Erickson zc.zc_cookie = received; 1607990b4856Slling if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) { 1608e9dbad6fSeschrock /* 1609e9dbad6fSeschrock * For user properties, the amount of work we have to do is very 1610e9dbad6fSeschrock * small, so just do it here. 1611e9dbad6fSeschrock */ 1612e9dbad6fSeschrock if (!zfs_prop_user(propname)) { 1613e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1614e9dbad6fSeschrock "invalid property")); 1615e9dbad6fSeschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 1616e9dbad6fSeschrock } 1617e9dbad6fSeschrock 1618e9dbad6fSeschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1619e9dbad6fSeschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 1620e9dbad6fSeschrock 1621e45ce728Sahrens if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0) 1622e9dbad6fSeschrock return (zfs_standard_error(hdl, errno, errbuf)); 1623e9dbad6fSeschrock 1624e9dbad6fSeschrock return (0); 1625e9dbad6fSeschrock } 1626e9dbad6fSeschrock 1627fa9e4066Sahrens /* 1628fa9e4066Sahrens * Verify that this property is inheritable. 1629fa9e4066Sahrens */ 163099653d4eSeschrock if (zfs_prop_readonly(prop)) 163199653d4eSeschrock return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf)); 1632fa9e4066Sahrens 163392241e0bSTom Erickson if (!zfs_prop_inheritable(prop) && !received) 163499653d4eSeschrock return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf)); 1635fa9e4066Sahrens 1636fa9e4066Sahrens /* 1637fa9e4066Sahrens * Check to see if the value applies to this type 1638fa9e4066Sahrens */ 163999653d4eSeschrock if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 164099653d4eSeschrock return (zfs_error(hdl, EZFS_PROPTYPE, errbuf)); 1641fa9e4066Sahrens 1642bf7c2d40Srm160521 /* 164336db6475SEric Taylor * Normalize the name, to get rid of shorthand abbreviations. 1644bf7c2d40Srm160521 */ 1645bf7c2d40Srm160521 propname = zfs_prop_to_name(prop); 1646fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1647e9dbad6fSeschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 1648fa9e4066Sahrens 1649fa9e4066Sahrens if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID && 1650fa9e4066Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 165199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 165299653d4eSeschrock "dataset is used in a non-global zone")); 165399653d4eSeschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 1654fa9e4066Sahrens } 1655fa9e4066Sahrens 1656fa9e4066Sahrens /* 1657fa9e4066Sahrens * Determine datasets which will be affected by this change, if any. 1658fa9e4066Sahrens */ 16590069fd67STim Haley if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL) 1660fa9e4066Sahrens return (-1); 1661fa9e4066Sahrens 1662fa9e4066Sahrens if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 166399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 166499653d4eSeschrock "child dataset with inherited mountpoint is used " 166599653d4eSeschrock "in a non-global zone")); 166699653d4eSeschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1667fa9e4066Sahrens goto error; 1668fa9e4066Sahrens } 1669fa9e4066Sahrens 1670fa9e4066Sahrens if ((ret = changelist_prefix(cl)) != 0) 1671fa9e4066Sahrens goto error; 1672fa9e4066Sahrens 1673e45ce728Sahrens if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) { 167499653d4eSeschrock return (zfs_standard_error(hdl, errno, errbuf)); 1675fa9e4066Sahrens } else { 1676fa9e4066Sahrens 1677efc555ebSnd150628 if ((ret = changelist_postfix(cl)) != 0) 1678fa9e4066Sahrens goto error; 1679fa9e4066Sahrens 1680fa9e4066Sahrens /* 1681fa9e4066Sahrens * Refresh the statistics so the new property is reflected. 1682fa9e4066Sahrens */ 1683fa9e4066Sahrens (void) get_stats(zhp); 1684fa9e4066Sahrens } 1685fa9e4066Sahrens 1686fa9e4066Sahrens error: 1687fa9e4066Sahrens changelist_free(cl); 1688fa9e4066Sahrens return (ret); 1689fa9e4066Sahrens } 1690fa9e4066Sahrens 1691fa9e4066Sahrens /* 16927f7322feSeschrock * True DSL properties are stored in an nvlist. The following two functions 16937f7322feSeschrock * extract them appropriately. 16947f7322feSeschrock */ 16957f7322feSeschrock static uint64_t 16967f7322feSeschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 16977f7322feSeschrock { 16987f7322feSeschrock nvlist_t *nv; 16997f7322feSeschrock uint64_t value; 17007f7322feSeschrock 1701a2eea2e1Sahrens *source = NULL; 17027f7322feSeschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 17037f7322feSeschrock zfs_prop_to_name(prop), &nv) == 0) { 1704990b4856Slling verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0); 1705990b4856Slling (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 17067f7322feSeschrock } else { 17072e5e9e19SSanjeev Bagewadi verify(!zhp->zfs_props_table || 17082e5e9e19SSanjeev Bagewadi zhp->zfs_props_table[prop] == B_TRUE); 17097f7322feSeschrock value = zfs_prop_default_numeric(prop); 17107f7322feSeschrock *source = ""; 17117f7322feSeschrock } 17127f7322feSeschrock 17137f7322feSeschrock return (value); 17147f7322feSeschrock } 17157f7322feSeschrock 17167f7322feSeschrock static char * 17177f7322feSeschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 17187f7322feSeschrock { 17197f7322feSeschrock nvlist_t *nv; 17207f7322feSeschrock char *value; 17217f7322feSeschrock 1722a2eea2e1Sahrens *source = NULL; 17237f7322feSeschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 17247f7322feSeschrock zfs_prop_to_name(prop), &nv) == 0) { 1725990b4856Slling verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0); 1726990b4856Slling (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 17277f7322feSeschrock } else { 17282e5e9e19SSanjeev Bagewadi verify(!zhp->zfs_props_table || 17292e5e9e19SSanjeev Bagewadi zhp->zfs_props_table[prop] == B_TRUE); 17307f7322feSeschrock if ((value = (char *)zfs_prop_default_string(prop)) == NULL) 17317f7322feSeschrock value = ""; 17327f7322feSeschrock *source = ""; 17337f7322feSeschrock } 17347f7322feSeschrock 17357f7322feSeschrock return (value); 17367f7322feSeschrock } 17377f7322feSeschrock 173892241e0bSTom Erickson static boolean_t 173992241e0bSTom Erickson zfs_is_recvd_props_mode(zfs_handle_t *zhp) 174092241e0bSTom Erickson { 174192241e0bSTom Erickson return (zhp->zfs_props == zhp->zfs_recvd_props); 174292241e0bSTom Erickson } 174392241e0bSTom Erickson 174492241e0bSTom Erickson static void 174592241e0bSTom Erickson zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie) 174692241e0bSTom Erickson { 174792241e0bSTom Erickson *cookie = (uint64_t)(uintptr_t)zhp->zfs_props; 174892241e0bSTom Erickson zhp->zfs_props = zhp->zfs_recvd_props; 174992241e0bSTom Erickson } 175092241e0bSTom Erickson 175192241e0bSTom Erickson static void 175292241e0bSTom Erickson zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie) 175392241e0bSTom Erickson { 175492241e0bSTom Erickson zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie; 175592241e0bSTom Erickson *cookie = 0; 175692241e0bSTom Erickson } 175792241e0bSTom Erickson 17587f7322feSeschrock /* 1759fa9e4066Sahrens * Internal function for getting a numeric property. Both zfs_prop_get() and 1760fa9e4066Sahrens * zfs_prop_get_int() are built using this interface. 1761fa9e4066Sahrens * 1762fa9e4066Sahrens * Certain properties can be overridden using 'mount -o'. In this case, scan 1763fa9e4066Sahrens * the contents of the /etc/mnttab entry, searching for the appropriate options. 1764fa9e4066Sahrens * If they differ from the on-disk values, report the current values and mark 1765fa9e4066Sahrens * the source "temporary". 1766fa9e4066Sahrens */ 176799653d4eSeschrock static int 1768990b4856Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src, 176999653d4eSeschrock char **source, uint64_t *val) 1770fa9e4066Sahrens { 1771bd00f61bSrm160521 zfs_cmd_t zc = { 0 }; 177296510749Stimh nvlist_t *zplprops = NULL; 1773fa9e4066Sahrens struct mnttab mnt; 17743ccfa83cSahrens char *mntopt_on = NULL; 17753ccfa83cSahrens char *mntopt_off = NULL; 177692241e0bSTom Erickson boolean_t received = zfs_is_recvd_props_mode(zhp); 1777fa9e4066Sahrens 1778fa9e4066Sahrens *source = NULL; 1779fa9e4066Sahrens 17803ccfa83cSahrens switch (prop) { 17813ccfa83cSahrens case ZFS_PROP_ATIME: 17823ccfa83cSahrens mntopt_on = MNTOPT_ATIME; 17833ccfa83cSahrens mntopt_off = MNTOPT_NOATIME; 17843ccfa83cSahrens break; 17853ccfa83cSahrens 17863ccfa83cSahrens case ZFS_PROP_DEVICES: 17873ccfa83cSahrens mntopt_on = MNTOPT_DEVICES; 17883ccfa83cSahrens mntopt_off = MNTOPT_NODEVICES; 17893ccfa83cSahrens break; 17903ccfa83cSahrens 17913ccfa83cSahrens case ZFS_PROP_EXEC: 17923ccfa83cSahrens mntopt_on = MNTOPT_EXEC; 17933ccfa83cSahrens mntopt_off = MNTOPT_NOEXEC; 17943ccfa83cSahrens break; 17953ccfa83cSahrens 17963ccfa83cSahrens case ZFS_PROP_READONLY: 17973ccfa83cSahrens mntopt_on = MNTOPT_RO; 17983ccfa83cSahrens mntopt_off = MNTOPT_RW; 17993ccfa83cSahrens break; 18003ccfa83cSahrens 18013ccfa83cSahrens case ZFS_PROP_SETUID: 18023ccfa83cSahrens mntopt_on = MNTOPT_SETUID; 18033ccfa83cSahrens mntopt_off = MNTOPT_NOSETUID; 18043ccfa83cSahrens break; 18053ccfa83cSahrens 18063ccfa83cSahrens case ZFS_PROP_XATTR: 18073ccfa83cSahrens mntopt_on = MNTOPT_XATTR; 18083ccfa83cSahrens mntopt_off = MNTOPT_NOXATTR; 18093ccfa83cSahrens break; 1810da6c28aaSamw 1811da6c28aaSamw case ZFS_PROP_NBMAND: 1812da6c28aaSamw mntopt_on = MNTOPT_NBMAND; 1813da6c28aaSamw mntopt_off = MNTOPT_NONBMAND; 1814da6c28aaSamw break; 18153ccfa83cSahrens } 18163ccfa83cSahrens 18173bb79becSeschrock /* 18183bb79becSeschrock * Because looking up the mount options is potentially expensive 18193bb79becSeschrock * (iterating over all of /etc/mnttab), we defer its calculation until 18203bb79becSeschrock * we're looking up a property which requires its presence. 18213bb79becSeschrock */ 18223bb79becSeschrock if (!zhp->zfs_mntcheck && 18233ccfa83cSahrens (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) { 1824ebedde84SEric Taylor libzfs_handle_t *hdl = zhp->zfs_hdl; 1825ebedde84SEric Taylor struct mnttab entry; 18263bb79becSeschrock 1827ebedde84SEric Taylor if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) { 1828ebedde84SEric Taylor zhp->zfs_mntopts = zfs_strdup(hdl, 18293ccfa83cSahrens entry.mnt_mntopts); 18303ccfa83cSahrens if (zhp->zfs_mntopts == NULL) 18313bb79becSeschrock return (-1); 18323ccfa83cSahrens } 18333bb79becSeschrock 18343bb79becSeschrock zhp->zfs_mntcheck = B_TRUE; 18353bb79becSeschrock } 18363bb79becSeschrock 1837fa9e4066Sahrens if (zhp->zfs_mntopts == NULL) 1838fa9e4066Sahrens mnt.mnt_mntopts = ""; 1839fa9e4066Sahrens else 1840fa9e4066Sahrens mnt.mnt_mntopts = zhp->zfs_mntopts; 1841fa9e4066Sahrens 1842fa9e4066Sahrens switch (prop) { 1843fa9e4066Sahrens case ZFS_PROP_ATIME: 1844fa9e4066Sahrens case ZFS_PROP_DEVICES: 1845fa9e4066Sahrens case ZFS_PROP_EXEC: 18463ccfa83cSahrens case ZFS_PROP_READONLY: 18473ccfa83cSahrens case ZFS_PROP_SETUID: 18483ccfa83cSahrens case ZFS_PROP_XATTR: 1849da6c28aaSamw case ZFS_PROP_NBMAND: 185099653d4eSeschrock *val = getprop_uint64(zhp, prop, source); 1851fa9e4066Sahrens 185292241e0bSTom Erickson if (received) 185392241e0bSTom Erickson break; 185492241e0bSTom Erickson 18553ccfa83cSahrens if (hasmntopt(&mnt, mntopt_on) && !*val) { 185699653d4eSeschrock *val = B_TRUE; 1857fa9e4066Sahrens if (src) 1858990b4856Slling *src = ZPROP_SRC_TEMPORARY; 18593ccfa83cSahrens } else if (hasmntopt(&mnt, mntopt_off) && *val) { 186099653d4eSeschrock *val = B_FALSE; 1861fa9e4066Sahrens if (src) 1862990b4856Slling *src = ZPROP_SRC_TEMPORARY; 1863fa9e4066Sahrens } 186499653d4eSeschrock break; 1865fa9e4066Sahrens 18663ccfa83cSahrens case ZFS_PROP_CANMOUNT: 1867d41c4376SMark J Musante case ZFS_PROP_VOLSIZE: 1868fa9e4066Sahrens case ZFS_PROP_QUOTA: 1869a9799022Sck153898 case ZFS_PROP_REFQUOTA: 1870fa9e4066Sahrens case ZFS_PROP_RESERVATION: 1871a9799022Sck153898 case ZFS_PROP_REFRESERVATION: 1872a2afb611SJerry Jelinek case ZFS_PROP_FILESYSTEM_LIMIT: 1873a2afb611SJerry Jelinek case ZFS_PROP_SNAPSHOT_LIMIT: 1874a2afb611SJerry Jelinek case ZFS_PROP_FILESYSTEM_COUNT: 1875a2afb611SJerry Jelinek case ZFS_PROP_SNAPSHOT_COUNT: 1876a2eea2e1Sahrens *val = getprop_uint64(zhp, prop, source); 187792241e0bSTom Erickson 187892241e0bSTom Erickson if (*source == NULL) { 187992241e0bSTom Erickson /* not default, must be local */ 1880fa9e4066Sahrens *source = zhp->zfs_name; 188192241e0bSTom Erickson } 188299653d4eSeschrock break; 1883fa9e4066Sahrens 1884fa9e4066Sahrens case ZFS_PROP_MOUNTED: 188599653d4eSeschrock *val = (zhp->zfs_mntopts != NULL); 188699653d4eSeschrock break; 1887fa9e4066Sahrens 188839c23413Seschrock case ZFS_PROP_NUMCLONES: 188939c23413Seschrock *val = zhp->zfs_dmustats.dds_num_clones; 189039c23413Seschrock break; 189139c23413Seschrock 1892bd00f61bSrm160521 case ZFS_PROP_VERSION: 1893de8267e0Stimh case ZFS_PROP_NORMALIZE: 1894de8267e0Stimh case ZFS_PROP_UTF8ONLY: 1895de8267e0Stimh case ZFS_PROP_CASE: 1896de8267e0Stimh if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) || 1897de8267e0Stimh zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 18983d7934e1Srm160521 return (-1); 1899bd00f61bSrm160521 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1900de8267e0Stimh if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) { 1901de8267e0Stimh zcmd_free_nvlists(&zc); 1902f4b94bdeSMatthew Ahrens return (-1); 1903bd00f61bSrm160521 } 1904de8267e0Stimh if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 || 1905de8267e0Stimh nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop), 1906de8267e0Stimh val) != 0) { 1907de8267e0Stimh zcmd_free_nvlists(&zc); 1908f4b94bdeSMatthew Ahrens return (-1); 1909de8267e0Stimh } 191096510749Stimh if (zplprops) 191196510749Stimh nvlist_free(zplprops); 1912de8267e0Stimh zcmd_free_nvlists(&zc); 1913bd00f61bSrm160521 break; 1914bd00f61bSrm160521 1915ca48f36fSKeith M Wesolowski case ZFS_PROP_INCONSISTENT: 1916ca48f36fSKeith M Wesolowski *val = zhp->zfs_dmustats.dds_inconsistent; 1917ca48f36fSKeith M Wesolowski break; 1918ca48f36fSKeith M Wesolowski 1919fa9e4066Sahrens default: 1920e7437265Sahrens switch (zfs_prop_get_type(prop)) { 192191ebeef5Sahrens case PROP_TYPE_NUMBER: 192291ebeef5Sahrens case PROP_TYPE_INDEX: 1923e7437265Sahrens *val = getprop_uint64(zhp, prop, source); 192474e7dc98SMatthew Ahrens /* 192514843421SMatthew Ahrens * If we tried to use a default value for a 192674e7dc98SMatthew Ahrens * readonly property, it means that it was not 192731f572c2STom Erickson * present. 192874e7dc98SMatthew Ahrens */ 192974e7dc98SMatthew Ahrens if (zfs_prop_readonly(prop) && 193031f572c2STom Erickson *source != NULL && (*source)[0] == '\0') { 193131f572c2STom Erickson *source = NULL; 193274e7dc98SMatthew Ahrens } 1933e7437265Sahrens break; 1934e7437265Sahrens 193591ebeef5Sahrens case PROP_TYPE_STRING: 1936e7437265Sahrens default: 193799653d4eSeschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 193899653d4eSeschrock "cannot get non-numeric property")); 193999653d4eSeschrock return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP, 194099653d4eSeschrock dgettext(TEXT_DOMAIN, "internal error"))); 1941fa9e4066Sahrens } 1942e7437265Sahrens } 1943fa9e4066Sahrens 1944fa9e4066Sahrens return (0); 1945fa9e4066Sahrens } 1946fa9e4066Sahrens 1947fa9e4066Sahrens /* 1948fa9e4066Sahrens * Calculate the source type, given the raw source string. 1949fa9e4066Sahrens */ 1950fa9e4066Sahrens static void 1951990b4856Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source, 1952fa9e4066Sahrens char *statbuf, size_t statlen) 1953fa9e4066Sahrens { 1954990b4856Slling if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY) 1955fa9e4066Sahrens return; 1956fa9e4066Sahrens 1957fa9e4066Sahrens if (source == NULL) { 1958990b4856Slling *srctype = ZPROP_SRC_NONE; 1959fa9e4066Sahrens } else if (source[0] == '\0') { 1960990b4856Slling *srctype = ZPROP_SRC_DEFAULT; 196192241e0bSTom Erickson } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) { 196292241e0bSTom Erickson *srctype = ZPROP_SRC_RECEIVED; 1963fa9e4066Sahrens } else { 1964fa9e4066Sahrens if (strcmp(source, zhp->zfs_name) == 0) { 1965990b4856Slling *srctype = ZPROP_SRC_LOCAL; 1966fa9e4066Sahrens } else { 1967fa9e4066Sahrens (void) strlcpy(statbuf, source, statlen); 1968990b4856Slling *srctype = ZPROP_SRC_INHERITED; 1969fa9e4066Sahrens } 1970fa9e4066Sahrens } 1971fa9e4066Sahrens 1972fa9e4066Sahrens } 1973fa9e4066Sahrens 197492241e0bSTom Erickson int 197592241e0bSTom Erickson zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf, 197692241e0bSTom Erickson size_t proplen, boolean_t literal) 197792241e0bSTom Erickson { 197892241e0bSTom Erickson zfs_prop_t prop; 197992241e0bSTom Erickson int err = 0; 198092241e0bSTom Erickson 198192241e0bSTom Erickson if (zhp->zfs_recvd_props == NULL) 198292241e0bSTom Erickson if (get_recvd_props_ioctl(zhp) != 0) 198392241e0bSTom Erickson return (-1); 198492241e0bSTom Erickson 198592241e0bSTom Erickson prop = zfs_name_to_prop(propname); 198692241e0bSTom Erickson 198792241e0bSTom Erickson if (prop != ZPROP_INVAL) { 198892241e0bSTom Erickson uint64_t cookie; 198992241e0bSTom Erickson if (!nvlist_exists(zhp->zfs_recvd_props, propname)) 199092241e0bSTom Erickson return (-1); 199192241e0bSTom Erickson zfs_set_recvd_props_mode(zhp, &cookie); 199292241e0bSTom Erickson err = zfs_prop_get(zhp, prop, propbuf, proplen, 199392241e0bSTom Erickson NULL, NULL, 0, literal); 199492241e0bSTom Erickson zfs_unset_recvd_props_mode(zhp, &cookie); 199592241e0bSTom Erickson } else { 199692241e0bSTom Erickson nvlist_t *propval; 199792241e0bSTom Erickson char *recvdval; 199892241e0bSTom Erickson if (nvlist_lookup_nvlist(zhp->zfs_recvd_props, 199992241e0bSTom Erickson propname, &propval) != 0) 200092241e0bSTom Erickson return (-1); 200192241e0bSTom Erickson verify(nvlist_lookup_string(propval, ZPROP_VALUE, 200292241e0bSTom Erickson &recvdval) == 0); 200392241e0bSTom Erickson (void) strlcpy(propbuf, recvdval, proplen); 200492241e0bSTom Erickson } 200592241e0bSTom Erickson 200692241e0bSTom Erickson return (err == 0 ? 0 : -1); 200792241e0bSTom Erickson } 200892241e0bSTom Erickson 200919b94df9SMatthew Ahrens static int 201019b94df9SMatthew Ahrens get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen) 201119b94df9SMatthew Ahrens { 201219b94df9SMatthew Ahrens nvlist_t *value; 201319b94df9SMatthew Ahrens nvpair_t *pair; 201419b94df9SMatthew Ahrens 201519b94df9SMatthew Ahrens value = zfs_get_clones_nvl(zhp); 201619b94df9SMatthew Ahrens if (value == NULL) 201719b94df9SMatthew Ahrens return (-1); 201819b94df9SMatthew Ahrens 201919b94df9SMatthew Ahrens propbuf[0] = '\0'; 202019b94df9SMatthew Ahrens for (pair = nvlist_next_nvpair(value, NULL); pair != NULL; 202119b94df9SMatthew Ahrens pair = nvlist_next_nvpair(value, pair)) { 202219b94df9SMatthew Ahrens if (propbuf[0] != '\0') 202319b94df9SMatthew Ahrens (void) strlcat(propbuf, ",", proplen); 202419b94df9SMatthew Ahrens (void) strlcat(propbuf, nvpair_name(pair), proplen); 202519b94df9SMatthew Ahrens } 202619b94df9SMatthew Ahrens 202719b94df9SMatthew Ahrens return (0); 202819b94df9SMatthew Ahrens } 202919b94df9SMatthew Ahrens 203019b94df9SMatthew Ahrens struct get_clones_arg { 203119b94df9SMatthew Ahrens uint64_t numclones; 203219b94df9SMatthew Ahrens nvlist_t *value; 203319b94df9SMatthew Ahrens const char *origin; 203419b94df9SMatthew Ahrens char buf[ZFS_MAXNAMELEN]; 203519b94df9SMatthew Ahrens }; 203619b94df9SMatthew Ahrens 203719b94df9SMatthew Ahrens int 203819b94df9SMatthew Ahrens get_clones_cb(zfs_handle_t *zhp, void *arg) 203919b94df9SMatthew Ahrens { 204019b94df9SMatthew Ahrens struct get_clones_arg *gca = arg; 204119b94df9SMatthew Ahrens 204219b94df9SMatthew Ahrens if (gca->numclones == 0) { 204319b94df9SMatthew Ahrens zfs_close(zhp); 204419b94df9SMatthew Ahrens return (0); 204519b94df9SMatthew Ahrens } 204619b94df9SMatthew Ahrens 204719b94df9SMatthew Ahrens if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf), 204819b94df9SMatthew Ahrens NULL, NULL, 0, B_TRUE) != 0) 204919b94df9SMatthew Ahrens goto out; 205019b94df9SMatthew Ahrens if (strcmp(gca->buf, gca->origin) == 0) { 20513b2aab18SMatthew Ahrens fnvlist_add_boolean(gca->value, zfs_get_name(zhp)); 205219b94df9SMatthew Ahrens gca->numclones--; 205319b94df9SMatthew Ahrens } 205419b94df9SMatthew Ahrens 205519b94df9SMatthew Ahrens out: 205619b94df9SMatthew Ahrens (void) zfs_iter_children(zhp, get_clones_cb, gca); 205719b94df9SMatthew Ahrens zfs_close(zhp); 205819b94df9SMatthew Ahrens return (0); 205919b94df9SMatthew Ahrens } 206019b94df9SMatthew Ahrens 206119b94df9SMatthew Ahrens nvlist_t * 206219b94df9SMatthew Ahrens zfs_get_clones_nvl(zfs_handle_t *zhp) 206319b94df9SMatthew Ahrens { 206419b94df9SMatthew Ahrens nvlist_t *nv, *value; 206519b94df9SMatthew Ahrens 206619b94df9SMatthew Ahrens if (nvlist_lookup_nvlist(zhp->zfs_props, 206719b94df9SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) { 206819b94df9SMatthew Ahrens struct get_clones_arg gca; 206919b94df9SMatthew Ahrens 207019b94df9SMatthew Ahrens /* 207119b94df9SMatthew Ahrens * if this is a snapshot, then the kernel wasn't able 207219b94df9SMatthew Ahrens * to get the clones. Do it by slowly iterating. 207319b94df9SMatthew Ahrens */ 207419b94df9SMatthew Ahrens if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) 207519b94df9SMatthew Ahrens return (NULL); 207619b94df9SMatthew Ahrens if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0) 207719b94df9SMatthew Ahrens return (NULL); 207819b94df9SMatthew Ahrens if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) { 207919b94df9SMatthew Ahrens nvlist_free(nv); 208019b94df9SMatthew Ahrens return (NULL); 208119b94df9SMatthew Ahrens } 208219b94df9SMatthew Ahrens 208319b94df9SMatthew Ahrens gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES); 208419b94df9SMatthew Ahrens gca.value = value; 208519b94df9SMatthew Ahrens gca.origin = zhp->zfs_name; 208619b94df9SMatthew Ahrens 208719b94df9SMatthew Ahrens if (gca.numclones != 0) { 208819b94df9SMatthew Ahrens zfs_handle_t *root; 208919b94df9SMatthew Ahrens char pool[ZFS_MAXNAMELEN]; 209019b94df9SMatthew Ahrens char *cp = pool; 209119b94df9SMatthew Ahrens 209219b94df9SMatthew Ahrens /* get the pool name */ 209319b94df9SMatthew Ahrens (void) strlcpy(pool, zhp->zfs_name, sizeof (pool)); 209419b94df9SMatthew Ahrens (void) strsep(&cp, "/@"); 209519b94df9SMatthew Ahrens root = zfs_open(zhp->zfs_hdl, pool, 209619b94df9SMatthew Ahrens ZFS_TYPE_FILESYSTEM); 209719b94df9SMatthew Ahrens 209819b94df9SMatthew Ahrens (void) get_clones_cb(root, &gca); 209919b94df9SMatthew Ahrens } 210019b94df9SMatthew Ahrens 210119b94df9SMatthew Ahrens if (gca.numclones != 0 || 210219b94df9SMatthew Ahrens nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 || 210319b94df9SMatthew Ahrens nvlist_add_nvlist(zhp->zfs_props, 210419b94df9SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) { 210519b94df9SMatthew Ahrens nvlist_free(nv); 210619b94df9SMatthew Ahrens nvlist_free(value); 210719b94df9SMatthew Ahrens return (NULL); 210819b94df9SMatthew Ahrens } 210919b94df9SMatthew Ahrens nvlist_free(nv); 211019b94df9SMatthew Ahrens nvlist_free(value); 211119b94df9SMatthew Ahrens verify(0 == nvlist_lookup_nvlist(zhp->zfs_props, 211219b94df9SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_CLONES), &nv)); 211319b94df9SMatthew Ahrens } 211419b94df9SMatthew Ahrens 211519b94df9SMatthew Ahrens verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0); 211619b94df9SMatthew Ahrens 211719b94df9SMatthew Ahrens return (value); 211819b94df9SMatthew Ahrens } 211919b94df9SMatthew Ahrens 2120fa9e4066Sahrens /* 2121fa9e4066Sahrens * Retrieve a property from the given object. If 'literal' is specified, then 2122fa9e4066Sahrens * numbers are left as exact values. Otherwise, numbers are converted to a 2123fa9e4066Sahrens * human-readable form. 2124fa9e4066Sahrens * 2125fa9e4066Sahrens * Returns 0 on success, or -1 on error. 2126fa9e4066Sahrens */ 2127fa9e4066Sahrens int 2128fa9e4066Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen, 2129990b4856Slling zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal) 2130fa9e4066Sahrens { 2131fa9e4066Sahrens char *source = NULL; 2132fa9e4066Sahrens uint64_t val; 2133fa9e4066Sahrens char *str; 2134e9dbad6fSeschrock const char *strval; 213592241e0bSTom Erickson boolean_t received = zfs_is_recvd_props_mode(zhp); 2136fa9e4066Sahrens 2137fa9e4066Sahrens /* 2138fa9e4066Sahrens * Check to see if this property applies to our object 2139fa9e4066Sahrens */ 2140fa9e4066Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 2141fa9e4066Sahrens return (-1); 2142fa9e4066Sahrens 214392241e0bSTom Erickson if (received && zfs_prop_readonly(prop)) 214492241e0bSTom Erickson return (-1); 214592241e0bSTom Erickson 2146fa9e4066Sahrens if (src) 2147990b4856Slling *src = ZPROP_SRC_NONE; 2148fa9e4066Sahrens 2149fa9e4066Sahrens switch (prop) { 2150fa9e4066Sahrens case ZFS_PROP_CREATION: 2151fa9e4066Sahrens /* 2152fa9e4066Sahrens * 'creation' is a time_t stored in the statistics. We convert 2153fa9e4066Sahrens * this into a string unless 'literal' is specified. 2154fa9e4066Sahrens */ 2155fa9e4066Sahrens { 2156a2eea2e1Sahrens val = getprop_uint64(zhp, prop, &source); 2157a2eea2e1Sahrens time_t time = (time_t)val; 2158fa9e4066Sahrens struct tm t; 2159fa9e4066Sahrens 2160fa9e4066Sahrens if (literal || 2161fa9e4066Sahrens localtime_r(&time, &t) == NULL || 2162fa9e4066Sahrens strftime(propbuf, proplen, "%a %b %e %k:%M %Y", 2163fa9e4066Sahrens &t) == 0) 2164a2eea2e1Sahrens (void) snprintf(propbuf, proplen, "%llu", val); 2165fa9e4066Sahrens } 2166fa9e4066Sahrens break; 2167fa9e4066Sahrens 2168fa9e4066Sahrens case ZFS_PROP_MOUNTPOINT: 2169fa9e4066Sahrens /* 2170fa9e4066Sahrens * Getting the precise mountpoint can be tricky. 2171fa9e4066Sahrens * 2172fa9e4066Sahrens * - for 'none' or 'legacy', return those values. 2173fa9e4066Sahrens * - for inherited mountpoints, we want to take everything 2174fa9e4066Sahrens * after our ancestor and append it to the inherited value. 2175fa9e4066Sahrens * 2176fa9e4066Sahrens * If the pool has an alternate root, we want to prepend that 2177fa9e4066Sahrens * root to any values we return. 2178fa9e4066Sahrens */ 217929ab75c9Srm160521 21807f7322feSeschrock str = getprop_string(zhp, prop, &source); 2181fa9e4066Sahrens 2182b21718f0Sgw25295 if (str[0] == '/') { 218329ab75c9Srm160521 char buf[MAXPATHLEN]; 218429ab75c9Srm160521 char *root = buf; 2185a79992aaSTom Erickson const char *relpath; 2186fa9e4066Sahrens 2187a79992aaSTom Erickson /* 2188a79992aaSTom Erickson * If we inherit the mountpoint, even from a dataset 2189a79992aaSTom Erickson * with a received value, the source will be the path of 2190a79992aaSTom Erickson * the dataset we inherit from. If source is 2191a79992aaSTom Erickson * ZPROP_SOURCE_VAL_RECVD, the received value is not 2192a79992aaSTom Erickson * inherited. 2193a79992aaSTom Erickson */ 2194a79992aaSTom Erickson if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) { 2195a79992aaSTom Erickson relpath = ""; 2196a79992aaSTom Erickson } else { 2197a79992aaSTom Erickson relpath = zhp->zfs_name + strlen(source); 2198fa9e4066Sahrens if (relpath[0] == '/') 2199fa9e4066Sahrens relpath++; 2200a79992aaSTom Erickson } 2201b21718f0Sgw25295 220229ab75c9Srm160521 if ((zpool_get_prop(zhp->zpool_hdl, 2203c58b3526SAdam Stevko ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL, 2204c58b3526SAdam Stevko B_FALSE)) || (strcmp(root, "-") == 0)) 220529ab75c9Srm160521 root[0] = '\0'; 2206b21718f0Sgw25295 /* 2207b21718f0Sgw25295 * Special case an alternate root of '/'. This will 2208b21718f0Sgw25295 * avoid having multiple leading slashes in the 2209b21718f0Sgw25295 * mountpoint path. 2210b21718f0Sgw25295 */ 2211b21718f0Sgw25295 if (strcmp(root, "/") == 0) 2212b21718f0Sgw25295 root++; 2213b21718f0Sgw25295 2214b21718f0Sgw25295 /* 2215b21718f0Sgw25295 * If the mountpoint is '/' then skip over this 2216b21718f0Sgw25295 * if we are obtaining either an alternate root or 2217b21718f0Sgw25295 * an inherited mountpoint. 2218b21718f0Sgw25295 */ 2219b21718f0Sgw25295 if (str[1] == '\0' && (root[0] != '\0' || 2220b21718f0Sgw25295 relpath[0] != '\0')) 22217f7322feSeschrock str++; 2222fa9e4066Sahrens 2223fa9e4066Sahrens if (relpath[0] == '\0') 2224fa9e4066Sahrens (void) snprintf(propbuf, proplen, "%s%s", 22257f7322feSeschrock root, str); 2226fa9e4066Sahrens else 2227fa9e4066Sahrens (void) snprintf(propbuf, proplen, "%s%s%s%s", 22287f7322feSeschrock root, str, relpath[0] == '@' ? "" : "/", 2229fa9e4066Sahrens relpath); 2230fa9e4066Sahrens } else { 2231fa9e4066Sahrens /* 'legacy' or 'none' */ 22327f7322feSeschrock (void) strlcpy(propbuf, str, proplen); 2233fa9e4066Sahrens } 2234fa9e4066Sahrens 2235fa9e4066Sahrens break; 2236fa9e4066Sahrens 2237fa9e4066Sahrens case ZFS_PROP_ORIGIN: 2238a2eea2e1Sahrens (void) strlcpy(propbuf, getprop_string(zhp, prop, &source), 2239fa9e4066Sahrens proplen); 2240fa9e4066Sahrens /* 2241fa9e4066Sahrens * If there is no parent at all, return failure to indicate that 2242fa9e4066Sahrens * it doesn't apply to this dataset. 2243fa9e4066Sahrens */ 2244fa9e4066Sahrens if (propbuf[0] == '\0') 2245fa9e4066Sahrens return (-1); 2246fa9e4066Sahrens break; 2247fa9e4066Sahrens 224819b94df9SMatthew Ahrens case ZFS_PROP_CLONES: 224919b94df9SMatthew Ahrens if (get_clones_string(zhp, propbuf, proplen) != 0) 225019b94df9SMatthew Ahrens return (-1); 225119b94df9SMatthew Ahrens break; 225219b94df9SMatthew Ahrens 2253fa9e4066Sahrens case ZFS_PROP_QUOTA: 2254a9799022Sck153898 case ZFS_PROP_REFQUOTA: 2255fa9e4066Sahrens case ZFS_PROP_RESERVATION: 2256a9799022Sck153898 case ZFS_PROP_REFRESERVATION: 2257a9799022Sck153898 225899653d4eSeschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 225999653d4eSeschrock return (-1); 2260fa9e4066Sahrens 2261fa9e4066Sahrens /* 2262fa9e4066Sahrens * If quota or reservation is 0, we translate this into 'none' 2263fa9e4066Sahrens * (unless literal is set), and indicate that it's the default 2264fa9e4066Sahrens * value. Otherwise, we print the number nicely and indicate 2265fa9e4066Sahrens * that its set locally. 2266fa9e4066Sahrens */ 2267fa9e4066Sahrens if (val == 0) { 2268fa9e4066Sahrens if (literal) 2269fa9e4066Sahrens (void) strlcpy(propbuf, "0", proplen); 2270fa9e4066Sahrens else 2271fa9e4066Sahrens (void) strlcpy(propbuf, "none", proplen); 2272fa9e4066Sahrens } else { 2273fa9e4066Sahrens if (literal) 22745ad82045Snd150628 (void) snprintf(propbuf, proplen, "%llu", 22755ad82045Snd150628 (u_longlong_t)val); 2276fa9e4066Sahrens else 2277fa9e4066Sahrens zfs_nicenum(val, propbuf, proplen); 2278fa9e4066Sahrens } 2279fa9e4066Sahrens break; 2280fa9e4066Sahrens 2281a2afb611SJerry Jelinek case ZFS_PROP_FILESYSTEM_LIMIT: 2282a2afb611SJerry Jelinek case ZFS_PROP_SNAPSHOT_LIMIT: 2283a2afb611SJerry Jelinek case ZFS_PROP_FILESYSTEM_COUNT: 2284a2afb611SJerry Jelinek case ZFS_PROP_SNAPSHOT_COUNT: 2285a2afb611SJerry Jelinek 2286a2afb611SJerry Jelinek if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 2287a2afb611SJerry Jelinek return (-1); 2288a2afb611SJerry Jelinek 2289a2afb611SJerry Jelinek /* 2290a2afb611SJerry Jelinek * If limit is UINT64_MAX, we translate this into 'none' (unless 2291a2afb611SJerry Jelinek * literal is set), and indicate that it's the default value. 2292a2afb611SJerry Jelinek * Otherwise, we print the number nicely and indicate that it's 2293a2afb611SJerry Jelinek * set locally. 2294a2afb611SJerry Jelinek */ 2295a2afb611SJerry Jelinek if (literal) { 2296a2afb611SJerry Jelinek (void) snprintf(propbuf, proplen, "%llu", 2297a2afb611SJerry Jelinek (u_longlong_t)val); 2298a2afb611SJerry Jelinek } else if (val == UINT64_MAX) { 2299a2afb611SJerry Jelinek (void) strlcpy(propbuf, "none", proplen); 2300a2afb611SJerry Jelinek } else { 2301a2afb611SJerry Jelinek zfs_nicenum(val, propbuf, proplen); 2302a2afb611SJerry Jelinek } 2303a2afb611SJerry Jelinek break; 2304a2afb611SJerry Jelinek 2305187d6ac0SMatt Ahrens case ZFS_PROP_REFRATIO: 2306fa9e4066Sahrens case ZFS_PROP_COMPRESSRATIO: 230799653d4eSeschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 230899653d4eSeschrock return (-1); 2309b24ab676SJeff Bonwick (void) snprintf(propbuf, proplen, "%llu.%02llux", 2310b24ab676SJeff Bonwick (u_longlong_t)(val / 100), 2311b24ab676SJeff Bonwick (u_longlong_t)(val % 100)); 2312fa9e4066Sahrens break; 2313fa9e4066Sahrens 2314fa9e4066Sahrens case ZFS_PROP_TYPE: 2315fa9e4066Sahrens switch (zhp->zfs_type) { 2316fa9e4066Sahrens case ZFS_TYPE_FILESYSTEM: 2317fa9e4066Sahrens str = "filesystem"; 2318fa9e4066Sahrens break; 2319fa9e4066Sahrens case ZFS_TYPE_VOLUME: 2320fa9e4066Sahrens str = "volume"; 2321fa9e4066Sahrens break; 2322fa9e4066Sahrens case ZFS_TYPE_SNAPSHOT: 2323fa9e4066Sahrens str = "snapshot"; 2324fa9e4066Sahrens break; 232578f17100SMatthew Ahrens case ZFS_TYPE_BOOKMARK: 232678f17100SMatthew Ahrens str = "bookmark"; 232778f17100SMatthew Ahrens break; 2328fa9e4066Sahrens default: 232999653d4eSeschrock abort(); 2330fa9e4066Sahrens } 2331fa9e4066Sahrens (void) snprintf(propbuf, proplen, "%s", str); 2332fa9e4066Sahrens break; 2333fa9e4066Sahrens 2334fa9e4066Sahrens case ZFS_PROP_MOUNTED: 2335fa9e4066Sahrens /* 2336fa9e4066Sahrens * The 'mounted' property is a pseudo-property that described 2337fa9e4066Sahrens * whether the filesystem is currently mounted. Even though 2338fa9e4066Sahrens * it's a boolean value, the typical values of "on" and "off" 2339fa9e4066Sahrens * don't make sense, so we translate to "yes" and "no". 2340fa9e4066Sahrens */ 234199653d4eSeschrock if (get_numeric_property(zhp, ZFS_PROP_MOUNTED, 234299653d4eSeschrock src, &source, &val) != 0) 234399653d4eSeschrock return (-1); 234499653d4eSeschrock if (val) 2345fa9e4066Sahrens (void) strlcpy(propbuf, "yes", proplen); 2346fa9e4066Sahrens else 2347fa9e4066Sahrens (void) strlcpy(propbuf, "no", proplen); 2348fa9e4066Sahrens break; 2349fa9e4066Sahrens 2350fa9e4066Sahrens case ZFS_PROP_NAME: 2351fa9e4066Sahrens /* 2352fa9e4066Sahrens * The 'name' property is a pseudo-property derived from the 2353fa9e4066Sahrens * dataset name. It is presented as a real property to simplify 2354fa9e4066Sahrens * consumers. 2355fa9e4066Sahrens */ 2356fa9e4066Sahrens (void) strlcpy(propbuf, zhp->zfs_name, proplen); 2357fa9e4066Sahrens break; 2358fa9e4066Sahrens 23594201a95eSRic Aleshire case ZFS_PROP_MLSLABEL: 23604201a95eSRic Aleshire { 23614201a95eSRic Aleshire m_label_t *new_sl = NULL; 23624201a95eSRic Aleshire char *ascii = NULL; /* human readable label */ 23634201a95eSRic Aleshire 23644201a95eSRic Aleshire (void) strlcpy(propbuf, 23654201a95eSRic Aleshire getprop_string(zhp, prop, &source), proplen); 23664201a95eSRic Aleshire 23674201a95eSRic Aleshire if (literal || (strcasecmp(propbuf, 23684201a95eSRic Aleshire ZFS_MLSLABEL_DEFAULT) == 0)) 23694201a95eSRic Aleshire break; 23704201a95eSRic Aleshire 23714201a95eSRic Aleshire /* 23724201a95eSRic Aleshire * Try to translate the internal hex string to 23734201a95eSRic Aleshire * human-readable output. If there are any 23744201a95eSRic Aleshire * problems just use the hex string. 23754201a95eSRic Aleshire */ 23764201a95eSRic Aleshire 23774201a95eSRic Aleshire if (str_to_label(propbuf, &new_sl, MAC_LABEL, 23784201a95eSRic Aleshire L_NO_CORRECTION, NULL) == -1) { 23794201a95eSRic Aleshire m_label_free(new_sl); 23804201a95eSRic Aleshire break; 23814201a95eSRic Aleshire } 23824201a95eSRic Aleshire 23834201a95eSRic Aleshire if (label_to_str(new_sl, &ascii, M_LABEL, 23844201a95eSRic Aleshire DEF_NAMES) != 0) { 23854201a95eSRic Aleshire if (ascii) 23864201a95eSRic Aleshire free(ascii); 23874201a95eSRic Aleshire m_label_free(new_sl); 23884201a95eSRic Aleshire break; 23894201a95eSRic Aleshire } 23904201a95eSRic Aleshire m_label_free(new_sl); 23914201a95eSRic Aleshire 23924201a95eSRic Aleshire (void) strlcpy(propbuf, ascii, proplen); 23934201a95eSRic Aleshire free(ascii); 23944201a95eSRic Aleshire } 23954201a95eSRic Aleshire break; 23964201a95eSRic Aleshire 2397f0f3ef5aSGarrett D'Amore case ZFS_PROP_GUID: 2398f0f3ef5aSGarrett D'Amore /* 2399f0f3ef5aSGarrett D'Amore * GUIDs are stored as numbers, but they are identifiers. 2400f0f3ef5aSGarrett D'Amore * We don't want them to be pretty printed, because pretty 2401f0f3ef5aSGarrett D'Amore * printing mangles the ID into a truncated and useless value. 2402f0f3ef5aSGarrett D'Amore */ 2403f0f3ef5aSGarrett D'Amore if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 2404f0f3ef5aSGarrett D'Amore return (-1); 2405f0f3ef5aSGarrett D'Amore (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val); 2406f0f3ef5aSGarrett D'Amore break; 2407f0f3ef5aSGarrett D'Amore 2408fa9e4066Sahrens default: 2409e7437265Sahrens switch (zfs_prop_get_type(prop)) { 241091ebeef5Sahrens case PROP_TYPE_NUMBER: 2411e7437265Sahrens if (get_numeric_property(zhp, prop, src, 2412e7437265Sahrens &source, &val) != 0) 2413e7437265Sahrens return (-1); 2414e7437265Sahrens if (literal) 2415e7437265Sahrens (void) snprintf(propbuf, proplen, "%llu", 2416e7437265Sahrens (u_longlong_t)val); 2417e7437265Sahrens else 2418e7437265Sahrens zfs_nicenum(val, propbuf, proplen); 2419e7437265Sahrens break; 2420e7437265Sahrens 242191ebeef5Sahrens case PROP_TYPE_STRING: 2422e7437265Sahrens (void) strlcpy(propbuf, 2423e7437265Sahrens getprop_string(zhp, prop, &source), proplen); 2424e7437265Sahrens break; 2425e7437265Sahrens 242691ebeef5Sahrens case PROP_TYPE_INDEX: 2427fe192f76Sahrens if (get_numeric_property(zhp, prop, src, 2428fe192f76Sahrens &source, &val) != 0) 2429fe192f76Sahrens return (-1); 2430fe192f76Sahrens if (zfs_prop_index_to_string(prop, val, &strval) != 0) 2431e7437265Sahrens return (-1); 2432e7437265Sahrens (void) strlcpy(propbuf, strval, proplen); 2433e7437265Sahrens break; 2434e7437265Sahrens 2435e7437265Sahrens default: 243699653d4eSeschrock abort(); 2437fa9e4066Sahrens } 2438e7437265Sahrens } 2439fa9e4066Sahrens 2440fa9e4066Sahrens get_source(zhp, src, source, statbuf, statlen); 2441fa9e4066Sahrens 2442fa9e4066Sahrens return (0); 2443fa9e4066Sahrens } 2444fa9e4066Sahrens 2445fa9e4066Sahrens /* 2446fa9e4066Sahrens * Utility function to get the given numeric property. Does no validation that 2447fa9e4066Sahrens * the given property is the appropriate type; should only be used with 2448fa9e4066Sahrens * hard-coded property types. 2449fa9e4066Sahrens */ 2450fa9e4066Sahrens uint64_t 2451fa9e4066Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop) 2452fa9e4066Sahrens { 2453fa9e4066Sahrens char *source; 245499653d4eSeschrock uint64_t val; 2455fa9e4066Sahrens 24563cb34c60Sahrens (void) get_numeric_property(zhp, prop, NULL, &source, &val); 245799653d4eSeschrock 245899653d4eSeschrock return (val); 2459fa9e4066Sahrens } 2460fa9e4066Sahrens 24617b97dc1aSrm160521 int 24627b97dc1aSrm160521 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val) 24637b97dc1aSrm160521 { 24647b97dc1aSrm160521 char buf[64]; 24657b97dc1aSrm160521 246614843421SMatthew Ahrens (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val); 24677b97dc1aSrm160521 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf)); 24687b97dc1aSrm160521 } 24697b97dc1aSrm160521 2470fa9e4066Sahrens /* 2471fa9e4066Sahrens * Similar to zfs_prop_get(), but returns the value as an integer. 2472fa9e4066Sahrens */ 2473fa9e4066Sahrens int 2474fa9e4066Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value, 2475990b4856Slling zprop_source_t *src, char *statbuf, size_t statlen) 2476fa9e4066Sahrens { 2477fa9e4066Sahrens char *source; 2478fa9e4066Sahrens 2479fa9e4066Sahrens /* 2480fa9e4066Sahrens * Check to see if this property applies to our object 2481fa9e4066Sahrens */ 2482e45ce728Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) { 2483ece3d9b3Slling return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE, 248499653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot get property '%s'"), 248599653d4eSeschrock zfs_prop_to_name(prop))); 2486e45ce728Sahrens } 2487fa9e4066Sahrens 2488fa9e4066Sahrens if (src) 2489990b4856Slling *src = ZPROP_SRC_NONE; 2490fa9e4066Sahrens 249199653d4eSeschrock if (get_numeric_property(zhp, prop, src, &source, value) != 0) 249299653d4eSeschrock return (-1); 2493fa9e4066Sahrens 2494fa9e4066Sahrens get_source(zhp, src, source, statbuf, statlen); 2495fa9e4066Sahrens 2496fa9e4066Sahrens return (0); 2497fa9e4066Sahrens } 2498fa9e4066Sahrens 249914843421SMatthew Ahrens static int 250014843421SMatthew Ahrens idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser, 250114843421SMatthew Ahrens char **domainp, idmap_rid_t *ridp) 250214843421SMatthew Ahrens { 250314843421SMatthew Ahrens idmap_get_handle_t *get_hdl = NULL; 250414843421SMatthew Ahrens idmap_stat status; 250514843421SMatthew Ahrens int err = EINVAL; 250614843421SMatthew Ahrens 25071fdeec65Sjoyce mcintosh if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS) 250814843421SMatthew Ahrens goto out; 250914843421SMatthew Ahrens 251014843421SMatthew Ahrens if (isuser) { 251114843421SMatthew Ahrens err = idmap_get_sidbyuid(get_hdl, id, 251214843421SMatthew Ahrens IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status); 251314843421SMatthew Ahrens } else { 251414843421SMatthew Ahrens err = idmap_get_sidbygid(get_hdl, id, 251514843421SMatthew Ahrens IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status); 251614843421SMatthew Ahrens } 251714843421SMatthew Ahrens if (err == IDMAP_SUCCESS && 251814843421SMatthew Ahrens idmap_get_mappings(get_hdl) == IDMAP_SUCCESS && 251914843421SMatthew Ahrens status == IDMAP_SUCCESS) 252014843421SMatthew Ahrens err = 0; 252114843421SMatthew Ahrens else 252214843421SMatthew Ahrens err = EINVAL; 252314843421SMatthew Ahrens out: 252414843421SMatthew Ahrens if (get_hdl) 252514843421SMatthew Ahrens idmap_get_destroy(get_hdl); 252614843421SMatthew Ahrens return (err); 252714843421SMatthew Ahrens } 252814843421SMatthew Ahrens 252914843421SMatthew Ahrens /* 253014843421SMatthew Ahrens * convert the propname into parameters needed by kernel 253114843421SMatthew Ahrens * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829 253214843421SMatthew Ahrens * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789 253314843421SMatthew Ahrens */ 253414843421SMatthew Ahrens static int 253514843421SMatthew Ahrens userquota_propname_decode(const char *propname, boolean_t zoned, 253614843421SMatthew Ahrens zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp) 253714843421SMatthew Ahrens { 253814843421SMatthew Ahrens zfs_userquota_prop_t type; 253914843421SMatthew Ahrens char *cp, *end; 25403b12c289SMatthew Ahrens char *numericsid = NULL; 254114843421SMatthew Ahrens boolean_t isuser; 254214843421SMatthew Ahrens 254314843421SMatthew Ahrens domain[0] = '\0'; 254414843421SMatthew Ahrens 254514843421SMatthew Ahrens /* Figure out the property type ({user|group}{quota|space}) */ 254614843421SMatthew Ahrens for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) { 254714843421SMatthew Ahrens if (strncmp(propname, zfs_userquota_prop_prefixes[type], 254814843421SMatthew Ahrens strlen(zfs_userquota_prop_prefixes[type])) == 0) 254914843421SMatthew Ahrens break; 255014843421SMatthew Ahrens } 255114843421SMatthew Ahrens if (type == ZFS_NUM_USERQUOTA_PROPS) 255214843421SMatthew Ahrens return (EINVAL); 255314843421SMatthew Ahrens *typep = type; 255414843421SMatthew Ahrens 255514843421SMatthew Ahrens isuser = (type == ZFS_PROP_USERQUOTA || 255614843421SMatthew Ahrens type == ZFS_PROP_USERUSED); 255714843421SMatthew Ahrens 255814843421SMatthew Ahrens cp = strchr(propname, '@') + 1; 255914843421SMatthew Ahrens 256014843421SMatthew Ahrens if (strchr(cp, '@')) { 256114843421SMatthew Ahrens /* 256214843421SMatthew Ahrens * It's a SID name (eg "user@domain") that needs to be 25633b12c289SMatthew Ahrens * turned into S-1-domainID-RID. 256414843421SMatthew Ahrens */ 25653b12c289SMatthew Ahrens directory_error_t e; 256614843421SMatthew Ahrens if (zoned && getzoneid() == GLOBAL_ZONEID) 256714843421SMatthew Ahrens return (ENOENT); 25683b12c289SMatthew Ahrens if (isuser) { 25693b12c289SMatthew Ahrens e = directory_sid_from_user_name(NULL, 25703b12c289SMatthew Ahrens cp, &numericsid); 25713b12c289SMatthew Ahrens } else { 25723b12c289SMatthew Ahrens e = directory_sid_from_group_name(NULL, 25733b12c289SMatthew Ahrens cp, &numericsid); 25743b12c289SMatthew Ahrens } 25753b12c289SMatthew Ahrens if (e != NULL) { 25763b12c289SMatthew Ahrens directory_error_free(e); 257714843421SMatthew Ahrens return (ENOENT); 25783b12c289SMatthew Ahrens } 25793b12c289SMatthew Ahrens if (numericsid == NULL) 258014843421SMatthew Ahrens return (ENOENT); 25813b12c289SMatthew Ahrens cp = numericsid; 25823b12c289SMatthew Ahrens /* will be further decoded below */ 25833b12c289SMatthew Ahrens } 25843b12c289SMatthew Ahrens 25853b12c289SMatthew Ahrens if (strncmp(cp, "S-1-", 4) == 0) { 258614843421SMatthew Ahrens /* It's a numeric SID (eg "S-1-234-567-89") */ 25873b12c289SMatthew Ahrens (void) strlcpy(domain, cp, domainlen); 258814843421SMatthew Ahrens cp = strrchr(domain, '-'); 258914843421SMatthew Ahrens *cp = '\0'; 259014843421SMatthew Ahrens cp++; 259114843421SMatthew Ahrens 259214843421SMatthew Ahrens errno = 0; 259314843421SMatthew Ahrens *ridp = strtoull(cp, &end, 10); 25943b12c289SMatthew Ahrens if (numericsid) { 25953b12c289SMatthew Ahrens free(numericsid); 25963b12c289SMatthew Ahrens numericsid = NULL; 25973b12c289SMatthew Ahrens } 2598777badbaSMatthew Ahrens if (errno != 0 || *end != '\0') 259914843421SMatthew Ahrens return (EINVAL); 260014843421SMatthew Ahrens } else if (!isdigit(*cp)) { 260114843421SMatthew Ahrens /* 260214843421SMatthew Ahrens * It's a user/group name (eg "user") that needs to be 260314843421SMatthew Ahrens * turned into a uid/gid 260414843421SMatthew Ahrens */ 260514843421SMatthew Ahrens if (zoned && getzoneid() == GLOBAL_ZONEID) 260614843421SMatthew Ahrens return (ENOENT); 260714843421SMatthew Ahrens if (isuser) { 260814843421SMatthew Ahrens struct passwd *pw; 260914843421SMatthew Ahrens pw = getpwnam(cp); 261014843421SMatthew Ahrens if (pw == NULL) 261114843421SMatthew Ahrens return (ENOENT); 261214843421SMatthew Ahrens *ridp = pw->pw_uid; 261314843421SMatthew Ahrens } else { 261414843421SMatthew Ahrens struct group *gr; 261514843421SMatthew Ahrens gr = getgrnam(cp); 261614843421SMatthew Ahrens if (gr == NULL) 261714843421SMatthew Ahrens return (ENOENT); 261814843421SMatthew Ahrens *ridp = gr->gr_gid; 261914843421SMatthew Ahrens } 262014843421SMatthew Ahrens } else { 262114843421SMatthew Ahrens /* It's a user/group ID (eg "12345"). */ 262214843421SMatthew Ahrens uid_t id = strtoul(cp, &end, 10); 262314843421SMatthew Ahrens idmap_rid_t rid; 262414843421SMatthew Ahrens char *mapdomain; 262514843421SMatthew Ahrens 262614843421SMatthew Ahrens if (*end != '\0') 262714843421SMatthew Ahrens return (EINVAL); 262814843421SMatthew Ahrens if (id > MAXUID) { 262914843421SMatthew Ahrens /* It's an ephemeral ID. */ 263014843421SMatthew Ahrens if (idmap_id_to_numeric_domain_rid(id, isuser, 263114843421SMatthew Ahrens &mapdomain, &rid) != 0) 263214843421SMatthew Ahrens return (ENOENT); 26333b12c289SMatthew Ahrens (void) strlcpy(domain, mapdomain, domainlen); 263414843421SMatthew Ahrens *ridp = rid; 263514843421SMatthew Ahrens } else { 263614843421SMatthew Ahrens *ridp = id; 263714843421SMatthew Ahrens } 263814843421SMatthew Ahrens } 263914843421SMatthew Ahrens 26403b12c289SMatthew Ahrens ASSERT3P(numericsid, ==, NULL); 264114843421SMatthew Ahrens return (0); 264214843421SMatthew Ahrens } 264314843421SMatthew Ahrens 2644edea4b55SLin Ling static int 2645edea4b55SLin Ling zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname, 2646edea4b55SLin Ling uint64_t *propvalue, zfs_userquota_prop_t *typep) 264714843421SMatthew Ahrens { 264814843421SMatthew Ahrens int err; 264914843421SMatthew Ahrens zfs_cmd_t zc = { 0 }; 265014843421SMatthew Ahrens 265119b94df9SMatthew Ahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 265214843421SMatthew Ahrens 265314843421SMatthew Ahrens err = userquota_propname_decode(propname, 265414843421SMatthew Ahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED), 2655edea4b55SLin Ling typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid); 2656edea4b55SLin Ling zc.zc_objset_type = *typep; 265714843421SMatthew Ahrens if (err) 265814843421SMatthew Ahrens return (err); 265914843421SMatthew Ahrens 266014843421SMatthew Ahrens err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc); 266114843421SMatthew Ahrens if (err) 266214843421SMatthew Ahrens return (err); 266314843421SMatthew Ahrens 2664edea4b55SLin Ling *propvalue = zc.zc_cookie; 2665edea4b55SLin Ling return (0); 2666edea4b55SLin Ling } 2667edea4b55SLin Ling 2668edea4b55SLin Ling int 2669edea4b55SLin Ling zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname, 2670edea4b55SLin Ling uint64_t *propvalue) 2671edea4b55SLin Ling { 2672edea4b55SLin Ling zfs_userquota_prop_t type; 2673edea4b55SLin Ling 2674edea4b55SLin Ling return (zfs_prop_get_userquota_common(zhp, propname, propvalue, 2675edea4b55SLin Ling &type)); 2676edea4b55SLin Ling } 2677edea4b55SLin Ling 2678edea4b55SLin Ling int 2679edea4b55SLin Ling zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname, 2680edea4b55SLin Ling char *propbuf, int proplen, boolean_t literal) 2681edea4b55SLin Ling { 2682edea4b55SLin Ling int err; 2683edea4b55SLin Ling uint64_t propvalue; 2684edea4b55SLin Ling zfs_userquota_prop_t type; 2685edea4b55SLin Ling 2686edea4b55SLin Ling err = zfs_prop_get_userquota_common(zhp, propname, &propvalue, 2687edea4b55SLin Ling &type); 2688edea4b55SLin Ling 2689edea4b55SLin Ling if (err) 2690edea4b55SLin Ling return (err); 2691edea4b55SLin Ling 269214843421SMatthew Ahrens if (literal) { 2693edea4b55SLin Ling (void) snprintf(propbuf, proplen, "%llu", propvalue); 2694edea4b55SLin Ling } else if (propvalue == 0 && 269514843421SMatthew Ahrens (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) { 269614843421SMatthew Ahrens (void) strlcpy(propbuf, "none", proplen); 269714843421SMatthew Ahrens } else { 2698edea4b55SLin Ling zfs_nicenum(propvalue, propbuf, proplen); 269914843421SMatthew Ahrens } 270014843421SMatthew Ahrens return (0); 270114843421SMatthew Ahrens } 270214843421SMatthew Ahrens 270319b94df9SMatthew Ahrens int 270419b94df9SMatthew Ahrens zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname, 270519b94df9SMatthew Ahrens uint64_t *propvalue) 270619b94df9SMatthew Ahrens { 270719b94df9SMatthew Ahrens int err; 270819b94df9SMatthew Ahrens zfs_cmd_t zc = { 0 }; 270919b94df9SMatthew Ahrens const char *snapname; 271019b94df9SMatthew Ahrens 271119b94df9SMatthew Ahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 271219b94df9SMatthew Ahrens 271319b94df9SMatthew Ahrens snapname = strchr(propname, '@') + 1; 271419b94df9SMatthew Ahrens if (strchr(snapname, '@')) { 271519b94df9SMatthew Ahrens (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value)); 271619b94df9SMatthew Ahrens } else { 271719b94df9SMatthew Ahrens /* snapname is the short name, append it to zhp's fsname */ 271819b94df9SMatthew Ahrens char *cp; 271919b94df9SMatthew Ahrens 272019b94df9SMatthew Ahrens (void) strlcpy(zc.zc_value, zhp->zfs_name, 272119b94df9SMatthew Ahrens sizeof (zc.zc_value)); 272219b94df9SMatthew Ahrens cp = strchr(zc.zc_value, '@'); 272319b94df9SMatthew Ahrens if (cp != NULL) 272419b94df9SMatthew Ahrens *cp = '\0'; 272519b94df9SMatthew Ahrens (void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value)); 272619b94df9SMatthew Ahrens (void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value)); 272719b94df9SMatthew Ahrens } 272819b94df9SMatthew Ahrens 272919b94df9SMatthew Ahrens err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc); 273019b94df9SMatthew Ahrens if (err) 273119b94df9SMatthew Ahrens return (err); 273219b94df9SMatthew Ahrens 273319b94df9SMatthew Ahrens *propvalue = zc.zc_cookie; 273419b94df9SMatthew Ahrens return (0); 273519b94df9SMatthew Ahrens } 273619b94df9SMatthew Ahrens 273719b94df9SMatthew Ahrens int 273819b94df9SMatthew Ahrens zfs_prop_get_written(zfs_handle_t *zhp, const char *propname, 273919b94df9SMatthew Ahrens char *propbuf, int proplen, boolean_t literal) 274019b94df9SMatthew Ahrens { 274119b94df9SMatthew Ahrens int err; 274219b94df9SMatthew Ahrens uint64_t propvalue; 274319b94df9SMatthew Ahrens 274419b94df9SMatthew Ahrens err = zfs_prop_get_written_int(zhp, propname, &propvalue); 274519b94df9SMatthew Ahrens 274619b94df9SMatthew Ahrens if (err) 274719b94df9SMatthew Ahrens return (err); 274819b94df9SMatthew Ahrens 274919b94df9SMatthew Ahrens if (literal) { 275019b94df9SMatthew Ahrens (void) snprintf(propbuf, proplen, "%llu", propvalue); 275119b94df9SMatthew Ahrens } else { 275219b94df9SMatthew Ahrens zfs_nicenum(propvalue, propbuf, proplen); 275319b94df9SMatthew Ahrens } 275419b94df9SMatthew Ahrens return (0); 275519b94df9SMatthew Ahrens } 275619b94df9SMatthew Ahrens 2757fa9e4066Sahrens /* 2758fa9e4066Sahrens * Returns the name of the given zfs handle. 2759fa9e4066Sahrens */ 2760fa9e4066Sahrens const char * 2761fa9e4066Sahrens zfs_get_name(const zfs_handle_t *zhp) 2762fa9e4066Sahrens { 2763fa9e4066Sahrens return (zhp->zfs_name); 2764fa9e4066Sahrens } 2765fa9e4066Sahrens 2766fa9e4066Sahrens /* 2767fa9e4066Sahrens * Returns the type of the given zfs handle. 2768fa9e4066Sahrens */ 2769fa9e4066Sahrens zfs_type_t 2770fa9e4066Sahrens zfs_get_type(const zfs_handle_t *zhp) 2771fa9e4066Sahrens { 2772fa9e4066Sahrens return (zhp->zfs_type); 2773fa9e4066Sahrens } 2774fa9e4066Sahrens 27757f7322feSeschrock /* 2776d41c4376SMark J Musante * Is one dataset name a child dataset of another? 2777d41c4376SMark J Musante * 2778d41c4376SMark J Musante * Needs to handle these cases: 2779d41c4376SMark J Musante * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo" 2780d41c4376SMark J Musante * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar" 2781d41c4376SMark J Musante * Descendant? No. No. No. Yes. 2782d41c4376SMark J Musante */ 2783d41c4376SMark J Musante static boolean_t 2784d41c4376SMark J Musante is_descendant(const char *ds1, const char *ds2) 2785d41c4376SMark J Musante { 2786d41c4376SMark J Musante size_t d1len = strlen(ds1); 2787d41c4376SMark J Musante 2788d41c4376SMark J Musante /* ds2 can't be a descendant if it's smaller */ 2789d41c4376SMark J Musante if (strlen(ds2) < d1len) 2790d41c4376SMark J Musante return (B_FALSE); 2791d41c4376SMark J Musante 2792d41c4376SMark J Musante /* otherwise, compare strings and verify that there's a '/' char */ 2793d41c4376SMark J Musante return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0)); 2794d41c4376SMark J Musante } 2795d41c4376SMark J Musante 2796d41c4376SMark J Musante /* 2797fa9e4066Sahrens * Given a complete name, return just the portion that refers to the parent. 279819b94df9SMatthew Ahrens * Will return -1 if there is no parent (path is just the name of the 279919b94df9SMatthew Ahrens * pool). 2800fa9e4066Sahrens */ 2801fa9e4066Sahrens static int 2802fa9e4066Sahrens parent_name(const char *path, char *buf, size_t buflen) 2803fa9e4066Sahrens { 280419b94df9SMatthew Ahrens char *slashp; 2805fa9e4066Sahrens 280619b94df9SMatthew Ahrens (void) strlcpy(buf, path, buflen); 280719b94df9SMatthew Ahrens 280819b94df9SMatthew Ahrens if ((slashp = strrchr(buf, '/')) == NULL) 2809fa9e4066Sahrens return (-1); 281019b94df9SMatthew Ahrens *slashp = '\0'; 2811fa9e4066Sahrens 2812fa9e4066Sahrens return (0); 2813fa9e4066Sahrens } 2814fa9e4066Sahrens 2815fa9e4066Sahrens /* 28167f1f55eaSvb160487 * If accept_ancestor is false, then check to make sure that the given path has 28177f1f55eaSvb160487 * a parent, and that it exists. If accept_ancestor is true, then find the 28187f1f55eaSvb160487 * closest existing ancestor for the given path. In prefixlen return the 28197f1f55eaSvb160487 * length of already existing prefix of the given path. We also fetch the 28207f1f55eaSvb160487 * 'zoned' property, which is used to validate property settings when creating 28217f1f55eaSvb160487 * new datasets. 2822fa9e4066Sahrens */ 2823fa9e4066Sahrens static int 28247f1f55eaSvb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned, 28257f1f55eaSvb160487 boolean_t accept_ancestor, int *prefixlen) 2826fa9e4066Sahrens { 2827fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 2828fa9e4066Sahrens char parent[ZFS_MAXNAMELEN]; 2829fa9e4066Sahrens char *slash; 28307f7322feSeschrock zfs_handle_t *zhp; 283199653d4eSeschrock char errbuf[1024]; 2832d41c4376SMark J Musante uint64_t is_zoned; 283399653d4eSeschrock 2834deb8317bSMark J Musante (void) snprintf(errbuf, sizeof (errbuf), 2835deb8317bSMark J Musante dgettext(TEXT_DOMAIN, "cannot create '%s'"), path); 2836fa9e4066Sahrens 2837fa9e4066Sahrens /* get parent, and check to see if this is just a pool */ 2838fa9e4066Sahrens if (parent_name(path, parent, sizeof (parent)) != 0) { 283999653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 284099653d4eSeschrock "missing dataset name")); 284199653d4eSeschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2842fa9e4066Sahrens } 2843fa9e4066Sahrens 2844fa9e4066Sahrens /* check to see if the pool exists */ 2845fa9e4066Sahrens if ((slash = strchr(parent, '/')) == NULL) 2846fa9e4066Sahrens slash = parent + strlen(parent); 28478ac09fceSRichard Lowe (void) strncpy(zc.zc_name, parent, slash - parent); 2848fa9e4066Sahrens zc.zc_name[slash - parent] = '\0'; 284999653d4eSeschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 && 2850fa9e4066Sahrens errno == ENOENT) { 285199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 285299653d4eSeschrock "no such pool '%s'"), zc.zc_name); 285399653d4eSeschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2854fa9e4066Sahrens } 2855fa9e4066Sahrens 2856fa9e4066Sahrens /* check to see if the parent dataset exists */ 28577f1f55eaSvb160487 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) { 28587f1f55eaSvb160487 if (errno == ENOENT && accept_ancestor) { 28597f1f55eaSvb160487 /* 28607f1f55eaSvb160487 * Go deeper to find an ancestor, give up on top level. 28617f1f55eaSvb160487 */ 28627f1f55eaSvb160487 if (parent_name(parent, parent, sizeof (parent)) != 0) { 28637f1f55eaSvb160487 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 28647f1f55eaSvb160487 "no such pool '%s'"), zc.zc_name); 28657f1f55eaSvb160487 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 28667f1f55eaSvb160487 } 28677f1f55eaSvb160487 } else if (errno == ENOENT) { 286899653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 286999653d4eSeschrock "parent does not exist")); 287099653d4eSeschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 28717f1f55eaSvb160487 } else 287299653d4eSeschrock return (zfs_standard_error(hdl, errno, errbuf)); 2873fa9e4066Sahrens } 2874fa9e4066Sahrens 2875d41c4376SMark J Musante is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); 2876d41c4376SMark J Musante if (zoned != NULL) 2877d41c4376SMark J Musante *zoned = is_zoned; 2878d41c4376SMark J Musante 2879fa9e4066Sahrens /* we are in a non-global zone, but parent is in the global zone */ 2880d41c4376SMark J Musante if (getzoneid() != GLOBAL_ZONEID && !is_zoned) { 288199653d4eSeschrock (void) zfs_standard_error(hdl, EPERM, errbuf); 28827f7322feSeschrock zfs_close(zhp); 2883fa9e4066Sahrens return (-1); 2884fa9e4066Sahrens } 2885fa9e4066Sahrens 2886fa9e4066Sahrens /* make sure parent is a filesystem */ 28877f7322feSeschrock if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) { 288899653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 288999653d4eSeschrock "parent is not a filesystem")); 289099653d4eSeschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 28917f7322feSeschrock zfs_close(zhp); 2892fa9e4066Sahrens return (-1); 2893fa9e4066Sahrens } 2894fa9e4066Sahrens 28957f7322feSeschrock zfs_close(zhp); 28967f1f55eaSvb160487 if (prefixlen != NULL) 28977f1f55eaSvb160487 *prefixlen = strlen(parent); 28987f1f55eaSvb160487 return (0); 28997f1f55eaSvb160487 } 29007f1f55eaSvb160487 29017f1f55eaSvb160487 /* 29027f1f55eaSvb160487 * Finds whether the dataset of the given type(s) exists. 29037f1f55eaSvb160487 */ 29047f1f55eaSvb160487 boolean_t 29057f1f55eaSvb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types) 29067f1f55eaSvb160487 { 29077f1f55eaSvb160487 zfs_handle_t *zhp; 29087f1f55eaSvb160487 2909f18faf3fSek110237 if (!zfs_validate_name(hdl, path, types, B_FALSE)) 29107f1f55eaSvb160487 return (B_FALSE); 29117f1f55eaSvb160487 29127f1f55eaSvb160487 /* 29137f1f55eaSvb160487 * Try to get stats for the dataset, which will tell us if it exists. 29147f1f55eaSvb160487 */ 29157f1f55eaSvb160487 if ((zhp = make_dataset_handle(hdl, path)) != NULL) { 29167f1f55eaSvb160487 int ds_type = zhp->zfs_type; 29177f1f55eaSvb160487 29187f1f55eaSvb160487 zfs_close(zhp); 29197f1f55eaSvb160487 if (types & ds_type) 29207f1f55eaSvb160487 return (B_TRUE); 29217f1f55eaSvb160487 } 29227f1f55eaSvb160487 return (B_FALSE); 29237f1f55eaSvb160487 } 29247f1f55eaSvb160487 29257f1f55eaSvb160487 /* 29263cb34c60Sahrens * Given a path to 'target', create all the ancestors between 29273cb34c60Sahrens * the prefixlen portion of the path, and the target itself. 29283cb34c60Sahrens * Fail if the initial prefixlen-ancestor does not already exist. 29293cb34c60Sahrens */ 29303cb34c60Sahrens int 29313cb34c60Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen) 29323cb34c60Sahrens { 29333cb34c60Sahrens zfs_handle_t *h; 29343cb34c60Sahrens char *cp; 29353cb34c60Sahrens const char *opname; 29363cb34c60Sahrens 29373cb34c60Sahrens /* make sure prefix exists */ 29383cb34c60Sahrens cp = target + prefixlen; 29393cb34c60Sahrens if (*cp != '/') { 29403cb34c60Sahrens assert(strchr(cp, '/') == NULL); 29413cb34c60Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 29423cb34c60Sahrens } else { 29433cb34c60Sahrens *cp = '\0'; 29443cb34c60Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 29453cb34c60Sahrens *cp = '/'; 29463cb34c60Sahrens } 29473cb34c60Sahrens if (h == NULL) 29483cb34c60Sahrens return (-1); 29493cb34c60Sahrens zfs_close(h); 29503cb34c60Sahrens 29513cb34c60Sahrens /* 29523cb34c60Sahrens * Attempt to create, mount, and share any ancestor filesystems, 29533cb34c60Sahrens * up to the prefixlen-long one. 29543cb34c60Sahrens */ 29553cb34c60Sahrens for (cp = target + prefixlen + 1; 29563cb34c60Sahrens cp = strchr(cp, '/'); *cp = '/', cp++) { 29573cb34c60Sahrens 29583cb34c60Sahrens *cp = '\0'; 29593cb34c60Sahrens 29603cb34c60Sahrens h = make_dataset_handle(hdl, target); 29613cb34c60Sahrens if (h) { 29623cb34c60Sahrens /* it already exists, nothing to do here */ 29633cb34c60Sahrens zfs_close(h); 29643cb34c60Sahrens continue; 29653cb34c60Sahrens } 29663cb34c60Sahrens 29673cb34c60Sahrens if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM, 29683cb34c60Sahrens NULL) != 0) { 29693cb34c60Sahrens opname = dgettext(TEXT_DOMAIN, "create"); 29703cb34c60Sahrens goto ancestorerr; 29713cb34c60Sahrens } 29723cb34c60Sahrens 29733cb34c60Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 29743cb34c60Sahrens if (h == NULL) { 29753cb34c60Sahrens opname = dgettext(TEXT_DOMAIN, "open"); 29763cb34c60Sahrens goto ancestorerr; 29773cb34c60Sahrens } 29783cb34c60Sahrens 29793cb34c60Sahrens if (zfs_mount(h, NULL, 0) != 0) { 29803cb34c60Sahrens opname = dgettext(TEXT_DOMAIN, "mount"); 29813cb34c60Sahrens goto ancestorerr; 29823cb34c60Sahrens } 29833cb34c60Sahrens 29843cb34c60Sahrens if (zfs_share(h) != 0) { 29853cb34c60Sahrens opname = dgettext(TEXT_DOMAIN, "share"); 29863cb34c60Sahrens goto ancestorerr; 29873cb34c60Sahrens } 29883cb34c60Sahrens 29893cb34c60Sahrens zfs_close(h); 29903cb34c60Sahrens } 29913cb34c60Sahrens 29923cb34c60Sahrens return (0); 29933cb34c60Sahrens 29943cb34c60Sahrens ancestorerr: 29953cb34c60Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29963cb34c60Sahrens "failed to %s ancestor '%s'"), opname, target); 29973cb34c60Sahrens return (-1); 29983cb34c60Sahrens } 29993cb34c60Sahrens 30003cb34c60Sahrens /* 30017f1f55eaSvb160487 * Creates non-existing ancestors of the given path. 30027f1f55eaSvb160487 */ 30037f1f55eaSvb160487 int 30047f1f55eaSvb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path) 30057f1f55eaSvb160487 { 30067f1f55eaSvb160487 int prefix; 30077f1f55eaSvb160487 char *path_copy; 30087f1f55eaSvb160487 int rc; 30097f1f55eaSvb160487 3010d41c4376SMark J Musante if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0) 30117f1f55eaSvb160487 return (-1); 30127f1f55eaSvb160487 30137f1f55eaSvb160487 if ((path_copy = strdup(path)) != NULL) { 30147f1f55eaSvb160487 rc = create_parents(hdl, path_copy, prefix); 30157f1f55eaSvb160487 free(path_copy); 30167f1f55eaSvb160487 } 30177f1f55eaSvb160487 if (path_copy == NULL || rc != 0) 30187f1f55eaSvb160487 return (-1); 30197f1f55eaSvb160487 3020fa9e4066Sahrens return (0); 3021fa9e4066Sahrens } 3022fa9e4066Sahrens 3023fa9e4066Sahrens /* 3024e9dbad6fSeschrock * Create a new filesystem or volume. 3025fa9e4066Sahrens */ 3026fa9e4066Sahrens int 302799653d4eSeschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type, 3028e9dbad6fSeschrock nvlist_t *props) 3029fa9e4066Sahrens { 3030fa9e4066Sahrens int ret; 3031fa9e4066Sahrens uint64_t size = 0; 3032fa9e4066Sahrens uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 303399653d4eSeschrock char errbuf[1024]; 3034e9dbad6fSeschrock uint64_t zoned; 30354445fffbSMatthew Ahrens dmu_objset_type_t ost; 303699653d4eSeschrock 303799653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 303899653d4eSeschrock "cannot create '%s'"), path); 3039fa9e4066Sahrens 3040fa9e4066Sahrens /* validate the path, taking care to note the extended error message */ 3041f18faf3fSek110237 if (!zfs_validate_name(hdl, path, type, B_TRUE)) 304299653d4eSeschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3043fa9e4066Sahrens 3044fa9e4066Sahrens /* validate parents exist */ 30457f1f55eaSvb160487 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0) 3046fa9e4066Sahrens return (-1); 3047fa9e4066Sahrens 3048fa9e4066Sahrens /* 3049fa9e4066Sahrens * The failure modes when creating a dataset of a different type over 3050fa9e4066Sahrens * one that already exists is a little strange. In particular, if you 3051fa9e4066Sahrens * try to create a dataset on top of an existing dataset, the ioctl() 3052fa9e4066Sahrens * will return ENOENT, not EEXIST. To prevent this from happening, we 3053fa9e4066Sahrens * first try to see if the dataset exists. 3054fa9e4066Sahrens */ 30554445fffbSMatthew Ahrens if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) { 305699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 305799653d4eSeschrock "dataset already exists")); 305899653d4eSeschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3059fa9e4066Sahrens } 3060fa9e4066Sahrens 3061fa9e4066Sahrens if (type == ZFS_TYPE_VOLUME) 30624445fffbSMatthew Ahrens ost = DMU_OST_ZVOL; 3063fa9e4066Sahrens else 30644445fffbSMatthew Ahrens ost = DMU_OST_ZFS; 3065fa9e4066Sahrens 30660a48a24eStimh if (props && (props = zfs_valid_proplist(hdl, type, props, 3067b1b8ab34Slling zoned, NULL, errbuf)) == 0) 3068e9dbad6fSeschrock return (-1); 3069e9dbad6fSeschrock 3070fa9e4066Sahrens if (type == ZFS_TYPE_VOLUME) { 30715c5460e9Seschrock /* 30725c5460e9Seschrock * If we are creating a volume, the size and block size must 30735c5460e9Seschrock * satisfy a few restraints. First, the blocksize must be a 30745c5460e9Seschrock * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the 30755c5460e9Seschrock * volsize must be a multiple of the block size, and cannot be 30765c5460e9Seschrock * zero. 30775c5460e9Seschrock */ 3078e9dbad6fSeschrock if (props == NULL || nvlist_lookup_uint64(props, 3079e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) { 3080e9dbad6fSeschrock nvlist_free(props); 308199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3082e9dbad6fSeschrock "missing volume size")); 3083e9dbad6fSeschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 3084fa9e4066Sahrens } 3085fa9e4066Sahrens 3086e9dbad6fSeschrock if ((ret = nvlist_lookup_uint64(props, 3087e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 3088e9dbad6fSeschrock &blocksize)) != 0) { 3089e9dbad6fSeschrock if (ret == ENOENT) { 3090e9dbad6fSeschrock blocksize = zfs_prop_default_numeric( 3091e9dbad6fSeschrock ZFS_PROP_VOLBLOCKSIZE); 3092e9dbad6fSeschrock } else { 3093e9dbad6fSeschrock nvlist_free(props); 309499653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3095e9dbad6fSeschrock "missing volume block size")); 3096e9dbad6fSeschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 3097e9dbad6fSeschrock } 3098e9dbad6fSeschrock } 3099e9dbad6fSeschrock 3100e9dbad6fSeschrock if (size == 0) { 3101e9dbad6fSeschrock nvlist_free(props); 3102e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3103e9dbad6fSeschrock "volume size cannot be zero")); 3104e9dbad6fSeschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 31055c5460e9Seschrock } 31065c5460e9Seschrock 31075c5460e9Seschrock if (size % blocksize != 0) { 3108e9dbad6fSeschrock nvlist_free(props); 310999653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3110e9dbad6fSeschrock "volume size must be a multiple of volume block " 3111e9dbad6fSeschrock "size")); 3112e9dbad6fSeschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 3113e9dbad6fSeschrock } 31145c5460e9Seschrock } 31155c5460e9Seschrock 3116fa9e4066Sahrens /* create the dataset */ 31174445fffbSMatthew Ahrens ret = lzc_create(path, ost, props); 31184445fffbSMatthew Ahrens nvlist_free(props); 3119e9dbad6fSeschrock 3120fa9e4066Sahrens /* check for failure */ 3121fa9e4066Sahrens if (ret != 0) { 3122fa9e4066Sahrens char parent[ZFS_MAXNAMELEN]; 3123fa9e4066Sahrens (void) parent_name(path, parent, sizeof (parent)); 3124fa9e4066Sahrens 3125fa9e4066Sahrens switch (errno) { 3126fa9e4066Sahrens case ENOENT: 312799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 312899653d4eSeschrock "no such parent '%s'"), parent); 312999653d4eSeschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 3130fa9e4066Sahrens 3131fa9e4066Sahrens case EINVAL: 313299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3133d7d4af51Smmusante "parent '%s' is not a filesystem"), parent); 313499653d4eSeschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 3135fa9e4066Sahrens 3136fa9e4066Sahrens case EDOM: 313799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3138e9dbad6fSeschrock "volume block size must be power of 2 from " 3139e9dbad6fSeschrock "%u to %uk"), 3140fa9e4066Sahrens (uint_t)SPA_MINBLOCKSIZE, 3141fa9e4066Sahrens (uint_t)SPA_MAXBLOCKSIZE >> 10); 314299653d4eSeschrock 3143e9dbad6fSeschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 314499653d4eSeschrock 314540feaa91Sahrens case ENOTSUP: 314640feaa91Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 314740feaa91Sahrens "pool must be upgraded to set this " 314840feaa91Sahrens "property or value")); 314940feaa91Sahrens return (zfs_error(hdl, EZFS_BADVERSION, errbuf)); 3150fa9e4066Sahrens #ifdef _ILP32 3151fa9e4066Sahrens case EOVERFLOW: 3152fa9e4066Sahrens /* 3153fa9e4066Sahrens * This platform can't address a volume this big. 3154fa9e4066Sahrens */ 315599653d4eSeschrock if (type == ZFS_TYPE_VOLUME) 315699653d4eSeschrock return (zfs_error(hdl, EZFS_VOLTOOBIG, 315799653d4eSeschrock errbuf)); 3158fa9e4066Sahrens #endif 315999653d4eSeschrock /* FALLTHROUGH */ 3160fa9e4066Sahrens default: 316199653d4eSeschrock return (zfs_standard_error(hdl, errno, errbuf)); 3162fa9e4066Sahrens } 3163fa9e4066Sahrens } 3164fa9e4066Sahrens 3165fa9e4066Sahrens return (0); 3166fa9e4066Sahrens } 3167fa9e4066Sahrens 3168fa9e4066Sahrens /* 3169fa9e4066Sahrens * Destroys the given dataset. The caller must make sure that the filesystem 317065fec9f6SChristopher Siden * isn't mounted, and that there are no active dependents. If the file system 317165fec9f6SChristopher Siden * does not exist this function does nothing. 3172fa9e4066Sahrens */ 3173fa9e4066Sahrens int 3174842727c2SChris Kirby zfs_destroy(zfs_handle_t *zhp, boolean_t defer) 3175fa9e4066Sahrens { 3176fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 3177fa9e4066Sahrens 317878f17100SMatthew Ahrens if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) { 317978f17100SMatthew Ahrens nvlist_t *nv = fnvlist_alloc(); 318078f17100SMatthew Ahrens fnvlist_add_boolean(nv, zhp->zfs_name); 318178f17100SMatthew Ahrens int error = lzc_destroy_bookmarks(nv, NULL); 318278f17100SMatthew Ahrens fnvlist_free(nv); 318378f17100SMatthew Ahrens if (error != 0) { 318478f17100SMatthew Ahrens return (zfs_standard_error_fmt(zhp->zfs_hdl, errno, 318578f17100SMatthew Ahrens dgettext(TEXT_DOMAIN, "cannot destroy '%s'"), 318678f17100SMatthew Ahrens zhp->zfs_name)); 318778f17100SMatthew Ahrens } 318878f17100SMatthew Ahrens return (0); 318978f17100SMatthew Ahrens } 319078f17100SMatthew Ahrens 3191fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3192fa9e4066Sahrens 3193e9dbad6fSeschrock if (ZFS_IS_VOLUME(zhp)) { 3194fa9e4066Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3195fa9e4066Sahrens } else { 3196fa9e4066Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3197fa9e4066Sahrens } 3198fa9e4066Sahrens 3199842727c2SChris Kirby zc.zc_defer_destroy = defer; 320065fec9f6SChristopher Siden if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0 && 320165fec9f6SChristopher Siden errno != ENOENT) { 3202ece3d9b3Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, errno, 320399653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot destroy '%s'"), 320499653d4eSeschrock zhp->zfs_name)); 32051d452cf5Sahrens } 3206fa9e4066Sahrens 3207fa9e4066Sahrens remove_mountpoint(zhp); 3208fa9e4066Sahrens 3209fa9e4066Sahrens return (0); 3210fa9e4066Sahrens } 3211fa9e4066Sahrens 32121d452cf5Sahrens struct destroydata { 321319b94df9SMatthew Ahrens nvlist_t *nvl; 321419b94df9SMatthew Ahrens const char *snapname; 32151d452cf5Sahrens }; 32161d452cf5Sahrens 32171d452cf5Sahrens static int 3218681d9761SEric Taylor zfs_check_snap_cb(zfs_handle_t *zhp, void *arg) 32191d452cf5Sahrens { 32201d452cf5Sahrens struct destroydata *dd = arg; 32211d452cf5Sahrens char name[ZFS_MAXNAMELEN]; 3222681d9761SEric Taylor int rv = 0; 32231d452cf5Sahrens 322419b94df9SMatthew Ahrens (void) snprintf(name, sizeof (name), 322519b94df9SMatthew Ahrens "%s@%s", zhp->zfs_name, dd->snapname); 32261d452cf5Sahrens 3227a7a845e4SSteven Hartland if (lzc_exists(name)) 322819b94df9SMatthew Ahrens verify(nvlist_add_boolean(dd->nvl, name) == 0); 32291d452cf5Sahrens 323019b94df9SMatthew Ahrens rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd); 32313ccfa83cSahrens zfs_close(zhp); 32323ccfa83cSahrens return (rv); 32331d452cf5Sahrens } 32341d452cf5Sahrens 32351d452cf5Sahrens /* 32361d452cf5Sahrens * Destroys all snapshots with the given name in zhp & descendants. 32371d452cf5Sahrens */ 32381d452cf5Sahrens int 3239842727c2SChris Kirby zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer) 32401d452cf5Sahrens { 32411d452cf5Sahrens int ret; 32421d452cf5Sahrens struct destroydata dd = { 0 }; 32431d452cf5Sahrens 32441d452cf5Sahrens dd.snapname = snapname; 324519b94df9SMatthew Ahrens verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0); 324619b94df9SMatthew Ahrens (void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd); 32471d452cf5Sahrens 3248a7a845e4SSteven Hartland if (nvlist_empty(dd.nvl)) { 324919b94df9SMatthew Ahrens ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT, 32501d452cf5Sahrens dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"), 325119b94df9SMatthew Ahrens zhp->zfs_name, snapname); 325219b94df9SMatthew Ahrens } else { 32533b2aab18SMatthew Ahrens ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer); 325419b94df9SMatthew Ahrens } 325519b94df9SMatthew Ahrens nvlist_free(dd.nvl); 325619b94df9SMatthew Ahrens return (ret); 3257e5351341SMatthew Ahrens } 3258e5351341SMatthew Ahrens 325919b94df9SMatthew Ahrens /* 32603b2aab18SMatthew Ahrens * Destroys all the snapshots named in the nvlist. 326119b94df9SMatthew Ahrens */ 326219b94df9SMatthew Ahrens int 32633b2aab18SMatthew Ahrens zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer) 326419b94df9SMatthew Ahrens { 326519b94df9SMatthew Ahrens int ret; 32664445fffbSMatthew Ahrens nvlist_t *errlist; 326719b94df9SMatthew Ahrens 32684445fffbSMatthew Ahrens ret = lzc_destroy_snaps(snaps, defer, &errlist); 32691d452cf5Sahrens 32703b2aab18SMatthew Ahrens if (ret == 0) 32713b2aab18SMatthew Ahrens return (0); 32723b2aab18SMatthew Ahrens 3273a7a845e4SSteven Hartland if (nvlist_empty(errlist)) { 32743b2aab18SMatthew Ahrens char errbuf[1024]; 32753b2aab18SMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), 32763b2aab18SMatthew Ahrens dgettext(TEXT_DOMAIN, "cannot destroy snapshots")); 32773b2aab18SMatthew Ahrens 32783b2aab18SMatthew Ahrens ret = zfs_standard_error(hdl, ret, errbuf); 32793b2aab18SMatthew Ahrens } 32804445fffbSMatthew Ahrens for (nvpair_t *pair = nvlist_next_nvpair(errlist, NULL); 32814445fffbSMatthew Ahrens pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) { 32821d452cf5Sahrens char errbuf[1024]; 32834445fffbSMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), 32844445fffbSMatthew Ahrens dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"), 32854445fffbSMatthew Ahrens nvpair_name(pair)); 32861d452cf5Sahrens 32874445fffbSMatthew Ahrens switch (fnvpair_value_int32(pair)) { 32881d452cf5Sahrens case EEXIST: 32893b2aab18SMatthew Ahrens zfs_error_aux(hdl, 32903b2aab18SMatthew Ahrens dgettext(TEXT_DOMAIN, "snapshot is cloned")); 32913b2aab18SMatthew Ahrens ret = zfs_error(hdl, EZFS_EXISTS, errbuf); 32924445fffbSMatthew Ahrens break; 32931d452cf5Sahrens default: 32943b2aab18SMatthew Ahrens ret = zfs_standard_error(hdl, errno, errbuf); 32954445fffbSMatthew Ahrens break; 32964445fffbSMatthew Ahrens } 32971d452cf5Sahrens } 32981d452cf5Sahrens 32994445fffbSMatthew Ahrens return (ret); 33001d452cf5Sahrens } 33011d452cf5Sahrens 3302fa9e4066Sahrens /* 3303fa9e4066Sahrens * Clones the given dataset. The target must be of the same type as the source. 3304fa9e4066Sahrens */ 3305fa9e4066Sahrens int 3306e9dbad6fSeschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props) 3307fa9e4066Sahrens { 3308fa9e4066Sahrens char parent[ZFS_MAXNAMELEN]; 3309fa9e4066Sahrens int ret; 331099653d4eSeschrock char errbuf[1024]; 331199653d4eSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 3312e9dbad6fSeschrock uint64_t zoned; 3313fa9e4066Sahrens 3314fa9e4066Sahrens assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 3315fa9e4066Sahrens 331699653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 331799653d4eSeschrock "cannot create '%s'"), target); 331899653d4eSeschrock 331919b94df9SMatthew Ahrens /* validate the target/clone name */ 3320f18faf3fSek110237 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE)) 332199653d4eSeschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3322fa9e4066Sahrens 3323fa9e4066Sahrens /* validate parents exist */ 33247f1f55eaSvb160487 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0) 3325fa9e4066Sahrens return (-1); 3326fa9e4066Sahrens 3327fa9e4066Sahrens (void) parent_name(target, parent, sizeof (parent)); 3328fa9e4066Sahrens 3329fa9e4066Sahrens /* do the clone */ 3330e9dbad6fSeschrock 3331e9dbad6fSeschrock if (props) { 33324445fffbSMatthew Ahrens zfs_type_t type; 33334445fffbSMatthew Ahrens if (ZFS_IS_VOLUME(zhp)) { 33344445fffbSMatthew Ahrens type = ZFS_TYPE_VOLUME; 33354445fffbSMatthew Ahrens } else { 33364445fffbSMatthew Ahrens type = ZFS_TYPE_FILESYSTEM; 33374445fffbSMatthew Ahrens } 33380a48a24eStimh if ((props = zfs_valid_proplist(hdl, type, props, zoned, 33390a48a24eStimh zhp, errbuf)) == NULL) 3340e9dbad6fSeschrock return (-1); 3341e9dbad6fSeschrock } 3342e9dbad6fSeschrock 33434445fffbSMatthew Ahrens ret = lzc_clone(target, zhp->zfs_name, props); 3344e9dbad6fSeschrock nvlist_free(props); 3345e9dbad6fSeschrock 3346fa9e4066Sahrens if (ret != 0) { 3347fa9e4066Sahrens switch (errno) { 3348fa9e4066Sahrens 3349fa9e4066Sahrens case ENOENT: 3350fa9e4066Sahrens /* 3351fa9e4066Sahrens * The parent doesn't exist. We should have caught this 3352fa9e4066Sahrens * above, but there may a race condition that has since 3353fa9e4066Sahrens * destroyed the parent. 3354fa9e4066Sahrens * 3355fa9e4066Sahrens * At this point, we don't know whether it's the source 3356fa9e4066Sahrens * that doesn't exist anymore, or whether the target 3357fa9e4066Sahrens * dataset doesn't exist. 3358fa9e4066Sahrens */ 335999653d4eSeschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 336099653d4eSeschrock "no such parent '%s'"), parent); 336199653d4eSeschrock return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf)); 3362fa9e4066Sahrens 336399653d4eSeschrock case EXDEV: 336499653d4eSeschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 336599653d4eSeschrock "source and target pools differ")); 336699653d4eSeschrock return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET, 336799653d4eSeschrock errbuf)); 336899653d4eSeschrock 336999653d4eSeschrock default: 337099653d4eSeschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 337199653d4eSeschrock errbuf)); 337299653d4eSeschrock } 337399653d4eSeschrock } 337499653d4eSeschrock 337599653d4eSeschrock return (ret); 337699653d4eSeschrock } 337799653d4eSeschrock 337899653d4eSeschrock /* 337999653d4eSeschrock * Promotes the given clone fs to be the clone parent. 338099653d4eSeschrock */ 338199653d4eSeschrock int 338299653d4eSeschrock zfs_promote(zfs_handle_t *zhp) 338399653d4eSeschrock { 338499653d4eSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 338599653d4eSeschrock zfs_cmd_t zc = { 0 }; 338699653d4eSeschrock char parent[MAXPATHLEN]; 338799653d4eSeschrock int ret; 338899653d4eSeschrock char errbuf[1024]; 338999653d4eSeschrock 339099653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 339199653d4eSeschrock "cannot promote '%s'"), zhp->zfs_name); 339299653d4eSeschrock 339399653d4eSeschrock if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 339499653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 339599653d4eSeschrock "snapshots can not be promoted")); 339699653d4eSeschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 339799653d4eSeschrock } 339899653d4eSeschrock 33993cb34c60Sahrens (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent)); 340099653d4eSeschrock if (parent[0] == '\0') { 340199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 340299653d4eSeschrock "not a cloned filesystem")); 340399653d4eSeschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 340499653d4eSeschrock } 340599653d4eSeschrock 34063cb34c60Sahrens (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin, 3407e9dbad6fSeschrock sizeof (zc.zc_value)); 340899653d4eSeschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3409ecd6cf80Smarks ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc); 341099653d4eSeschrock 341199653d4eSeschrock if (ret != 0) { 34120b69c2f0Sahrens int save_errno = errno; 3413fa9e4066Sahrens 34140b69c2f0Sahrens switch (save_errno) { 3415fa9e4066Sahrens case EEXIST: 3416681d9761SEric Taylor /* There is a conflicting snapshot name. */ 341799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3418681d9761SEric Taylor "conflicting snapshot '%s' from parent '%s'"), 3419681d9761SEric Taylor zc.zc_string, parent); 342099653d4eSeschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3421fa9e4066Sahrens 3422fa9e4066Sahrens default: 34230b69c2f0Sahrens return (zfs_standard_error(hdl, save_errno, errbuf)); 3424fa9e4066Sahrens } 3425fa9e4066Sahrens } 3426e9dbad6fSeschrock return (ret); 34271d452cf5Sahrens } 34281d452cf5Sahrens 34294445fffbSMatthew Ahrens typedef struct snapdata { 34304445fffbSMatthew Ahrens nvlist_t *sd_nvl; 34314445fffbSMatthew Ahrens const char *sd_snapname; 34324445fffbSMatthew Ahrens } snapdata_t; 34334445fffbSMatthew Ahrens 34344445fffbSMatthew Ahrens static int 34354445fffbSMatthew Ahrens zfs_snapshot_cb(zfs_handle_t *zhp, void *arg) 34364445fffbSMatthew Ahrens { 34374445fffbSMatthew Ahrens snapdata_t *sd = arg; 34384445fffbSMatthew Ahrens char name[ZFS_MAXNAMELEN]; 34394445fffbSMatthew Ahrens int rv = 0; 34404445fffbSMatthew Ahrens 3441ca48f36fSKeith M Wesolowski if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) { 34424445fffbSMatthew Ahrens (void) snprintf(name, sizeof (name), 34434445fffbSMatthew Ahrens "%s@%s", zfs_get_name(zhp), sd->sd_snapname); 34444445fffbSMatthew Ahrens 34454445fffbSMatthew Ahrens fnvlist_add_boolean(sd->sd_nvl, name); 34464445fffbSMatthew Ahrens 34474445fffbSMatthew Ahrens rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd); 3448ca48f36fSKeith M Wesolowski } 34494445fffbSMatthew Ahrens zfs_close(zhp); 3450ca48f36fSKeith M Wesolowski 34514445fffbSMatthew Ahrens return (rv); 34524445fffbSMatthew Ahrens } 34534445fffbSMatthew Ahrens 3454fa9e4066Sahrens /* 34554445fffbSMatthew Ahrens * Creates snapshots. The keys in the snaps nvlist are the snapshots to be 34564445fffbSMatthew Ahrens * created. 3457fa9e4066Sahrens */ 3458fa9e4066Sahrens int 34594445fffbSMatthew Ahrens zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props) 34604445fffbSMatthew Ahrens { 34614445fffbSMatthew Ahrens int ret; 34624445fffbSMatthew Ahrens char errbuf[1024]; 34634445fffbSMatthew Ahrens nvpair_t *elem; 34644445fffbSMatthew Ahrens nvlist_t *errors; 34654445fffbSMatthew Ahrens 34664445fffbSMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 34674445fffbSMatthew Ahrens "cannot create snapshots ")); 34684445fffbSMatthew Ahrens 34694445fffbSMatthew Ahrens elem = NULL; 34704445fffbSMatthew Ahrens while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) { 34714445fffbSMatthew Ahrens const char *snapname = nvpair_name(elem); 34724445fffbSMatthew Ahrens 34734445fffbSMatthew Ahrens /* validate the target name */ 34744445fffbSMatthew Ahrens if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT, 34754445fffbSMatthew Ahrens B_TRUE)) { 34764445fffbSMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), 34774445fffbSMatthew Ahrens dgettext(TEXT_DOMAIN, 34784445fffbSMatthew Ahrens "cannot create snapshot '%s'"), snapname); 34794445fffbSMatthew Ahrens return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 34804445fffbSMatthew Ahrens } 34814445fffbSMatthew Ahrens } 34824445fffbSMatthew Ahrens 34834445fffbSMatthew Ahrens if (props != NULL && 34844445fffbSMatthew Ahrens (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT, 34854445fffbSMatthew Ahrens props, B_FALSE, NULL, errbuf)) == NULL) { 34864445fffbSMatthew Ahrens return (-1); 34874445fffbSMatthew Ahrens } 34884445fffbSMatthew Ahrens 34894445fffbSMatthew Ahrens ret = lzc_snapshot(snaps, props, &errors); 34904445fffbSMatthew Ahrens 34914445fffbSMatthew Ahrens if (ret != 0) { 34924445fffbSMatthew Ahrens boolean_t printed = B_FALSE; 34934445fffbSMatthew Ahrens for (elem = nvlist_next_nvpair(errors, NULL); 34944445fffbSMatthew Ahrens elem != NULL; 34954445fffbSMatthew Ahrens elem = nvlist_next_nvpair(errors, elem)) { 34964445fffbSMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), 34974445fffbSMatthew Ahrens dgettext(TEXT_DOMAIN, 34984445fffbSMatthew Ahrens "cannot create snapshot '%s'"), nvpair_name(elem)); 34994445fffbSMatthew Ahrens (void) zfs_standard_error(hdl, 35004445fffbSMatthew Ahrens fnvpair_value_int32(elem), errbuf); 35014445fffbSMatthew Ahrens printed = B_TRUE; 35024445fffbSMatthew Ahrens } 35034445fffbSMatthew Ahrens if (!printed) { 35044445fffbSMatthew Ahrens switch (ret) { 35054445fffbSMatthew Ahrens case EXDEV: 35064445fffbSMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35074445fffbSMatthew Ahrens "multiple snapshots of same " 35084445fffbSMatthew Ahrens "fs not allowed")); 35094445fffbSMatthew Ahrens (void) zfs_error(hdl, EZFS_EXISTS, errbuf); 35104445fffbSMatthew Ahrens 35114445fffbSMatthew Ahrens break; 35124445fffbSMatthew Ahrens default: 35134445fffbSMatthew Ahrens (void) zfs_standard_error(hdl, ret, errbuf); 35144445fffbSMatthew Ahrens } 35154445fffbSMatthew Ahrens } 35164445fffbSMatthew Ahrens } 35174445fffbSMatthew Ahrens 35184445fffbSMatthew Ahrens nvlist_free(props); 35194445fffbSMatthew Ahrens nvlist_free(errors); 35204445fffbSMatthew Ahrens return (ret); 35214445fffbSMatthew Ahrens } 35224445fffbSMatthew Ahrens 35234445fffbSMatthew Ahrens int 3524bb0ade09Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive, 3525bb0ade09Sahrens nvlist_t *props) 3526fa9e4066Sahrens { 3527fa9e4066Sahrens int ret; 35284445fffbSMatthew Ahrens snapdata_t sd = { 0 }; 35294445fffbSMatthew Ahrens char fsname[ZFS_MAXNAMELEN]; 35304445fffbSMatthew Ahrens char *cp; 35314445fffbSMatthew Ahrens zfs_handle_t *zhp; 353299653d4eSeschrock char errbuf[1024]; 3533fa9e4066Sahrens 353499653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 35354445fffbSMatthew Ahrens "cannot snapshot %s"), path); 353699653d4eSeschrock 3537f18faf3fSek110237 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE)) 353899653d4eSeschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3539fa9e4066Sahrens 35404445fffbSMatthew Ahrens (void) strlcpy(fsname, path, sizeof (fsname)); 35414445fffbSMatthew Ahrens cp = strchr(fsname, '@'); 35424445fffbSMatthew Ahrens *cp = '\0'; 35434445fffbSMatthew Ahrens sd.sd_snapname = cp + 1; 3544bb0ade09Sahrens 35454445fffbSMatthew Ahrens if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | 3546fa9e4066Sahrens ZFS_TYPE_VOLUME)) == NULL) { 3547fa9e4066Sahrens return (-1); 3548fa9e4066Sahrens } 3549fa9e4066Sahrens 35504445fffbSMatthew Ahrens verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0); 35514445fffbSMatthew Ahrens if (recursive) { 35524445fffbSMatthew Ahrens (void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd); 35534445fffbSMatthew Ahrens } else { 35544445fffbSMatthew Ahrens fnvlist_add_boolean(sd.sd_nvl, path); 3555681d9761SEric Taylor } 3556fa9e4066Sahrens 35574445fffbSMatthew Ahrens ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props); 35584445fffbSMatthew Ahrens nvlist_free(sd.sd_nvl); 3559fa9e4066Sahrens zfs_close(zhp); 3560fa9e4066Sahrens return (ret); 3561fa9e4066Sahrens } 3562fa9e4066Sahrens 3563fa9e4066Sahrens /* 3564b12a1c38Slling * Destroy any more recent snapshots. We invoke this callback on any dependents 3565b12a1c38Slling * of the snapshot first. If the 'cb_dependent' member is non-zero, then this 3566b12a1c38Slling * is a dependent and we should just destroy it without checking the transaction 3567b12a1c38Slling * group. 3568fa9e4066Sahrens */ 3569b12a1c38Slling typedef struct rollback_data { 3570b12a1c38Slling const char *cb_target; /* the snapshot */ 3571b12a1c38Slling uint64_t cb_create; /* creation time reference */ 3572c391e322Sahrens boolean_t cb_error; 3573c391e322Sahrens boolean_t cb_force; 3574b12a1c38Slling } rollback_data_t; 3575b12a1c38Slling 3576b12a1c38Slling static int 357778f17100SMatthew Ahrens rollback_destroy_dependent(zfs_handle_t *zhp, void *data) 3578b12a1c38Slling { 3579b12a1c38Slling rollback_data_t *cbp = data; 3580c391e322Sahrens prop_changelist_t *clp; 3581c391e322Sahrens 358278f17100SMatthew Ahrens /* We must destroy this clone; first unmount it */ 35830069fd67STim Haley clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 3584c391e322Sahrens cbp->cb_force ? MS_FORCE: 0); 3585c391e322Sahrens if (clp == NULL || changelist_prefix(clp) != 0) { 3586c391e322Sahrens cbp->cb_error = B_TRUE; 3587c391e322Sahrens zfs_close(zhp); 3588c391e322Sahrens return (0); 3589c391e322Sahrens } 3590842727c2SChris Kirby if (zfs_destroy(zhp, B_FALSE) != 0) 3591c391e322Sahrens cbp->cb_error = B_TRUE; 3592c391e322Sahrens else 3593c391e322Sahrens changelist_remove(clp, zhp->zfs_name); 3594ba7b046eSahrens (void) changelist_postfix(clp); 3595c391e322Sahrens changelist_free(clp); 359678f17100SMatthew Ahrens 359778f17100SMatthew Ahrens zfs_close(zhp); 359878f17100SMatthew Ahrens return (0); 359978f17100SMatthew Ahrens } 360078f17100SMatthew Ahrens 360178f17100SMatthew Ahrens static int 360278f17100SMatthew Ahrens rollback_destroy(zfs_handle_t *zhp, void *data) 360378f17100SMatthew Ahrens { 360478f17100SMatthew Ahrens rollback_data_t *cbp = data; 360578f17100SMatthew Ahrens 360678f17100SMatthew Ahrens if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) { 360778f17100SMatthew Ahrens cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE, 360878f17100SMatthew Ahrens rollback_destroy_dependent, cbp); 360978f17100SMatthew Ahrens 361078f17100SMatthew Ahrens cbp->cb_error |= zfs_destroy(zhp, B_FALSE); 3611b12a1c38Slling } 3612b12a1c38Slling 3613b12a1c38Slling zfs_close(zhp); 3614b12a1c38Slling return (0); 3615b12a1c38Slling } 3616b12a1c38Slling 3617b12a1c38Slling /* 36184ccbb6e7Sahrens * Given a dataset, rollback to a specific snapshot, discarding any 36194ccbb6e7Sahrens * data changes since then and making it the active dataset. 36204ccbb6e7Sahrens * 362178f17100SMatthew Ahrens * Any snapshots and bookmarks more recent than the target are 362278f17100SMatthew Ahrens * destroyed, along with their dependents (i.e. clones). 3623b12a1c38Slling */ 36244ccbb6e7Sahrens int 3625c391e322Sahrens zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force) 3626fa9e4066Sahrens { 36274ccbb6e7Sahrens rollback_data_t cb = { 0 }; 36284ccbb6e7Sahrens int err; 36297b97dc1aSrm160521 boolean_t restore_resv = 0; 36307b97dc1aSrm160521 uint64_t old_volsize, new_volsize; 36317b97dc1aSrm160521 zfs_prop_t resv_prop; 3632fa9e4066Sahrens 3633fa9e4066Sahrens assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM || 3634fa9e4066Sahrens zhp->zfs_type == ZFS_TYPE_VOLUME); 3635fa9e4066Sahrens 36364ccbb6e7Sahrens /* 36372e2c1355SMatthew Ahrens * Destroy all recent snapshots and their dependents. 36384ccbb6e7Sahrens */ 3639c391e322Sahrens cb.cb_force = force; 36404ccbb6e7Sahrens cb.cb_target = snap->zfs_name; 36414ccbb6e7Sahrens cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG); 364278f17100SMatthew Ahrens (void) zfs_iter_snapshots(zhp, rollback_destroy, &cb); 364378f17100SMatthew Ahrens (void) zfs_iter_bookmarks(zhp, rollback_destroy, &cb); 36444ccbb6e7Sahrens 3645c391e322Sahrens if (cb.cb_error) 3646c391e322Sahrens return (-1); 36474ccbb6e7Sahrens 36484ccbb6e7Sahrens /* 36494ccbb6e7Sahrens * Now that we have verified that the snapshot is the latest, 36504ccbb6e7Sahrens * rollback to the given snapshot. 36514ccbb6e7Sahrens */ 36524ccbb6e7Sahrens 36537b97dc1aSrm160521 if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 36547b97dc1aSrm160521 if (zfs_which_resv_prop(zhp, &resv_prop) < 0) 36557b97dc1aSrm160521 return (-1); 36567b97dc1aSrm160521 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 36577b97dc1aSrm160521 restore_resv = 36587b97dc1aSrm160521 (old_volsize == zfs_prop_get_int(zhp, resv_prop)); 36597b97dc1aSrm160521 } 3660fa9e4066Sahrens 3661fa9e4066Sahrens /* 36624ccbb6e7Sahrens * We rely on zfs_iter_children() to verify that there are no 36634ccbb6e7Sahrens * newer snapshots for the given dataset. Therefore, we can 36644ccbb6e7Sahrens * simply pass the name on to the ioctl() call. There is still 36654ccbb6e7Sahrens * an unlikely race condition where the user has taken a 36664ccbb6e7Sahrens * snapshot since we verified that this was the most recent. 3667fa9e4066Sahrens */ 3668a7027df1SMatthew Ahrens err = lzc_rollback(zhp->zfs_name, NULL, 0); 3669a7027df1SMatthew Ahrens if (err != 0) { 3670ece3d9b3Slling (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno, 367199653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot rollback '%s'"), 367299653d4eSeschrock zhp->zfs_name); 3673b9415e83Srm160521 return (err); 3674b9415e83Srm160521 } 3675fa9e4066Sahrens 36767b97dc1aSrm160521 /* 36777b97dc1aSrm160521 * For volumes, if the pre-rollback volsize matched the pre- 36787b97dc1aSrm160521 * rollback reservation and the volsize has changed then set 36797b97dc1aSrm160521 * the reservation property to the post-rollback volsize. 36807b97dc1aSrm160521 * Make a new handle since the rollback closed the dataset. 36817b97dc1aSrm160521 */ 3682b9415e83Srm160521 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) && 3683b9415e83Srm160521 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) { 36847b97dc1aSrm160521 if (restore_resv) { 36857b97dc1aSrm160521 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 36867b97dc1aSrm160521 if (old_volsize != new_volsize) 3687b9415e83Srm160521 err = zfs_prop_set_int(zhp, resv_prop, 3688b9415e83Srm160521 new_volsize); 36897b97dc1aSrm160521 } 36907b97dc1aSrm160521 zfs_close(zhp); 36917b97dc1aSrm160521 } 36924ccbb6e7Sahrens return (err); 3693b12a1c38Slling } 3694b12a1c38Slling 3695b12a1c38Slling /* 3696fa9e4066Sahrens * Renames the given dataset. 3697fa9e4066Sahrens */ 3698fa9e4066Sahrens int 36996a9cb0eaSEric Schrock zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive, 37006a9cb0eaSEric Schrock boolean_t force_unmount) 3701fa9e4066Sahrens { 3702fa9e4066Sahrens int ret; 3703fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 3704fa9e4066Sahrens char *delim; 3705cdf5b4caSmmusante prop_changelist_t *cl = NULL; 3706cdf5b4caSmmusante zfs_handle_t *zhrp = NULL; 3707cdf5b4caSmmusante char *parentname = NULL; 3708fa9e4066Sahrens char parent[ZFS_MAXNAMELEN]; 370999653d4eSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 371099653d4eSeschrock char errbuf[1024]; 3711fa9e4066Sahrens 3712fa9e4066Sahrens /* if we have the same exact name, just return success */ 3713fa9e4066Sahrens if (strcmp(zhp->zfs_name, target) == 0) 3714fa9e4066Sahrens return (0); 3715fa9e4066Sahrens 371699653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 371799653d4eSeschrock "cannot rename to '%s'"), target); 371899653d4eSeschrock 3719fa9e4066Sahrens /* 3720fa9e4066Sahrens * Make sure the target name is valid 3721fa9e4066Sahrens */ 3722fa9e4066Sahrens if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 372398579b20Snd150628 if ((strchr(target, '@') == NULL) || 372498579b20Snd150628 *target == '@') { 372598579b20Snd150628 /* 372698579b20Snd150628 * Snapshot target name is abbreviated, 372798579b20Snd150628 * reconstruct full dataset name 372898579b20Snd150628 */ 372998579b20Snd150628 (void) strlcpy(parent, zhp->zfs_name, 373098579b20Snd150628 sizeof (parent)); 373198579b20Snd150628 delim = strchr(parent, '@'); 373298579b20Snd150628 if (strchr(target, '@') == NULL) 373398579b20Snd150628 *(++delim) = '\0'; 373498579b20Snd150628 else 373598579b20Snd150628 *delim = '\0'; 373698579b20Snd150628 (void) strlcat(parent, target, sizeof (parent)); 373798579b20Snd150628 target = parent; 373898579b20Snd150628 } else { 3739fa9e4066Sahrens /* 3740fa9e4066Sahrens * Make sure we're renaming within the same dataset. 3741fa9e4066Sahrens */ 374298579b20Snd150628 delim = strchr(target, '@'); 374398579b20Snd150628 if (strncmp(zhp->zfs_name, target, delim - target) 374498579b20Snd150628 != 0 || zhp->zfs_name[delim - target] != '@') { 374599653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 374698579b20Snd150628 "snapshots must be part of same " 374798579b20Snd150628 "dataset")); 374898579b20Snd150628 return (zfs_error(hdl, EZFS_CROSSTARGET, 374998579b20Snd150628 errbuf)); 3750fa9e4066Sahrens } 375198579b20Snd150628 } 3752f18faf3fSek110237 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 375398579b20Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3754fa9e4066Sahrens } else { 3755cdf5b4caSmmusante if (recursive) { 3756cdf5b4caSmmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3757cdf5b4caSmmusante "recursive rename must be a snapshot")); 3758cdf5b4caSmmusante return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 3759cdf5b4caSmmusante } 3760cdf5b4caSmmusante 3761f18faf3fSek110237 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 376298579b20Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3763e9dbad6fSeschrock 3764fa9e4066Sahrens /* validate parents */ 3765d41c4376SMark J Musante if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0) 3766fa9e4066Sahrens return (-1); 3767fa9e4066Sahrens 3768fa9e4066Sahrens /* make sure we're in the same pool */ 3769fa9e4066Sahrens verify((delim = strchr(target, '/')) != NULL); 3770fa9e4066Sahrens if (strncmp(zhp->zfs_name, target, delim - target) != 0 || 3771fa9e4066Sahrens zhp->zfs_name[delim - target] != '/') { 377299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 377399653d4eSeschrock "datasets must be within same pool")); 377499653d4eSeschrock return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 3775fa9e4066Sahrens } 3776f2fdf992Snd150628 3777f2fdf992Snd150628 /* new name cannot be a child of the current dataset name */ 3778d41c4376SMark J Musante if (is_descendant(zhp->zfs_name, target)) { 3779f2fdf992Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3780d41c4376SMark J Musante "New dataset name cannot be a descendant of " 3781f2fdf992Snd150628 "current dataset name")); 3782f2fdf992Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3783f2fdf992Snd150628 } 3784fa9e4066Sahrens } 3785fa9e4066Sahrens 378699653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), 378799653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name); 378899653d4eSeschrock 3789fa9e4066Sahrens if (getzoneid() == GLOBAL_ZONEID && 3790fa9e4066Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 379199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 379299653d4eSeschrock "dataset is used in a non-global zone")); 379399653d4eSeschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 3794fa9e4066Sahrens } 3795fa9e4066Sahrens 3796cdf5b4caSmmusante if (recursive) { 3797f0c5ee21Smmusante parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name); 3798f0c5ee21Smmusante if (parentname == NULL) { 3799f0c5ee21Smmusante ret = -1; 3800f0c5ee21Smmusante goto error; 3801f0c5ee21Smmusante } 3802cdf5b4caSmmusante delim = strchr(parentname, '@'); 3803cdf5b4caSmmusante *delim = '\0'; 3804990b4856Slling zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET); 3805cdf5b4caSmmusante if (zhrp == NULL) { 3806f0c5ee21Smmusante ret = -1; 3807f0c5ee21Smmusante goto error; 3808cdf5b4caSmmusante } 3809*33cde0d0SMatthew Ahrens } else if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) { 38106a9cb0eaSEric Schrock if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0, 38116a9cb0eaSEric Schrock force_unmount ? MS_FORCE : 0)) == NULL) 381299653d4eSeschrock return (-1); 3813fa9e4066Sahrens 3814fa9e4066Sahrens if (changelist_haszonedchild(cl)) { 381599653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 381699653d4eSeschrock "child dataset with inherited mountpoint is used " 381799653d4eSeschrock "in a non-global zone")); 3818e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_ZONED, errbuf); 3819fa9e4066Sahrens goto error; 3820fa9e4066Sahrens } 3821fa9e4066Sahrens 3822fa9e4066Sahrens if ((ret = changelist_prefix(cl)) != 0) 3823fa9e4066Sahrens goto error; 3824cdf5b4caSmmusante } 3825fa9e4066Sahrens 3826e9dbad6fSeschrock if (ZFS_IS_VOLUME(zhp)) 3827fa9e4066Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3828fa9e4066Sahrens else 3829fa9e4066Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3830fa9e4066Sahrens 383198579b20Snd150628 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3832e9dbad6fSeschrock (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value)); 383398579b20Snd150628 3834cdf5b4caSmmusante zc.zc_cookie = recursive; 3835cdf5b4caSmmusante 3836ecd6cf80Smarks if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) { 3837cdf5b4caSmmusante /* 3838cdf5b4caSmmusante * if it was recursive, the one that actually failed will 3839cdf5b4caSmmusante * be in zc.zc_name 3840cdf5b4caSmmusante */ 3841cdf5b4caSmmusante (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 38423cb34c60Sahrens "cannot rename '%s'"), zc.zc_name); 3843cdf5b4caSmmusante 3844cdf5b4caSmmusante if (recursive && errno == EEXIST) { 3845cdf5b4caSmmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3846cdf5b4caSmmusante "a child dataset already has a snapshot " 3847cdf5b4caSmmusante "with the new name")); 3848a10acbd6Seschrock (void) zfs_error(hdl, EZFS_EXISTS, errbuf); 3849cdf5b4caSmmusante } else { 385099653d4eSeschrock (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf); 3851cdf5b4caSmmusante } 3852fa9e4066Sahrens 3853fa9e4066Sahrens /* 3854fa9e4066Sahrens * On failure, we still want to remount any filesystems that 3855fa9e4066Sahrens * were previously mounted, so we don't alter the system state. 3856fa9e4066Sahrens */ 3857*33cde0d0SMatthew Ahrens if (cl != NULL) 3858fa9e4066Sahrens (void) changelist_postfix(cl); 3859cdf5b4caSmmusante } else { 3860*33cde0d0SMatthew Ahrens if (cl != NULL) { 3861fa9e4066Sahrens changelist_rename(cl, zfs_get_name(zhp), target); 3862fa9e4066Sahrens ret = changelist_postfix(cl); 3863fa9e4066Sahrens } 3864cdf5b4caSmmusante } 3865fa9e4066Sahrens 3866fa9e4066Sahrens error: 3867*33cde0d0SMatthew Ahrens if (parentname != NULL) { 3868cdf5b4caSmmusante free(parentname); 3869cdf5b4caSmmusante } 3870*33cde0d0SMatthew Ahrens if (zhrp != NULL) { 3871cdf5b4caSmmusante zfs_close(zhrp); 3872cdf5b4caSmmusante } 3873*33cde0d0SMatthew Ahrens if (cl != NULL) { 3874fa9e4066Sahrens changelist_free(cl); 3875cdf5b4caSmmusante } 3876fa9e4066Sahrens return (ret); 3877fa9e4066Sahrens } 3878fa9e4066Sahrens 3879e9dbad6fSeschrock nvlist_t * 3880e9dbad6fSeschrock zfs_get_user_props(zfs_handle_t *zhp) 3881e9dbad6fSeschrock { 3882e9dbad6fSeschrock return (zhp->zfs_user_props); 3883e9dbad6fSeschrock } 3884e9dbad6fSeschrock 388592241e0bSTom Erickson nvlist_t * 388692241e0bSTom Erickson zfs_get_recvd_props(zfs_handle_t *zhp) 388792241e0bSTom Erickson { 388892241e0bSTom Erickson if (zhp->zfs_recvd_props == NULL) 388992241e0bSTom Erickson if (get_recvd_props_ioctl(zhp) != 0) 389092241e0bSTom Erickson return (NULL); 389192241e0bSTom Erickson return (zhp->zfs_recvd_props); 389292241e0bSTom Erickson } 389392241e0bSTom Erickson 3894e9dbad6fSeschrock /* 3895e9dbad6fSeschrock * This function is used by 'zfs list' to determine the exact set of columns to 3896e9dbad6fSeschrock * display, and their maximum widths. This does two main things: 3897e9dbad6fSeschrock * 3898e9dbad6fSeschrock * - If this is a list of all properties, then expand the list to include 3899e9dbad6fSeschrock * all native properties, and set a flag so that for each dataset we look 3900e9dbad6fSeschrock * for new unique user properties and add them to the list. 3901e9dbad6fSeschrock * 3902e9dbad6fSeschrock * - For non fixed-width properties, keep track of the maximum width seen 390392241e0bSTom Erickson * so that we can size the column appropriately. If the user has 390492241e0bSTom Erickson * requested received property values, we also need to compute the width 390592241e0bSTom Erickson * of the RECEIVED column. 3906e9dbad6fSeschrock */ 3907e9dbad6fSeschrock int 390843d68d68SYuri Pankov zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received, 390943d68d68SYuri Pankov boolean_t literal) 3910e9dbad6fSeschrock { 3911e9dbad6fSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 3912990b4856Slling zprop_list_t *entry; 3913990b4856Slling zprop_list_t **last, **start; 3914e9dbad6fSeschrock nvlist_t *userprops, *propval; 3915e9dbad6fSeschrock nvpair_t *elem; 3916e9dbad6fSeschrock char *strval; 3917e9dbad6fSeschrock char buf[ZFS_MAXPROPLEN]; 3918e9dbad6fSeschrock 3919990b4856Slling if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0) 3920e9dbad6fSeschrock return (-1); 3921e9dbad6fSeschrock 3922e9dbad6fSeschrock userprops = zfs_get_user_props(zhp); 3923e9dbad6fSeschrock 3924e9dbad6fSeschrock entry = *plp; 3925e9dbad6fSeschrock if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) { 3926e9dbad6fSeschrock /* 3927e9dbad6fSeschrock * Go through and add any user properties as necessary. We 3928e9dbad6fSeschrock * start by incrementing our list pointer to the first 3929e9dbad6fSeschrock * non-native property. 3930e9dbad6fSeschrock */ 3931e9dbad6fSeschrock start = plp; 3932e9dbad6fSeschrock while (*start != NULL) { 3933990b4856Slling if ((*start)->pl_prop == ZPROP_INVAL) 3934e9dbad6fSeschrock break; 3935e9dbad6fSeschrock start = &(*start)->pl_next; 3936e9dbad6fSeschrock } 3937e9dbad6fSeschrock 3938e9dbad6fSeschrock elem = NULL; 3939e9dbad6fSeschrock while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) { 3940e9dbad6fSeschrock /* 3941e9dbad6fSeschrock * See if we've already found this property in our list. 3942e9dbad6fSeschrock */ 3943e9dbad6fSeschrock for (last = start; *last != NULL; 3944e9dbad6fSeschrock last = &(*last)->pl_next) { 3945e9dbad6fSeschrock if (strcmp((*last)->pl_user_prop, 3946e9dbad6fSeschrock nvpair_name(elem)) == 0) 3947e9dbad6fSeschrock break; 3948e9dbad6fSeschrock } 3949e9dbad6fSeschrock 3950e9dbad6fSeschrock if (*last == NULL) { 3951e9dbad6fSeschrock if ((entry = zfs_alloc(hdl, 3952990b4856Slling sizeof (zprop_list_t))) == NULL || 3953e9dbad6fSeschrock ((entry->pl_user_prop = zfs_strdup(hdl, 3954e9dbad6fSeschrock nvpair_name(elem)))) == NULL) { 3955e9dbad6fSeschrock free(entry); 3956e9dbad6fSeschrock return (-1); 3957e9dbad6fSeschrock } 3958e9dbad6fSeschrock 3959990b4856Slling entry->pl_prop = ZPROP_INVAL; 3960e9dbad6fSeschrock entry->pl_width = strlen(nvpair_name(elem)); 3961e9dbad6fSeschrock entry->pl_all = B_TRUE; 3962e9dbad6fSeschrock *last = entry; 3963e9dbad6fSeschrock } 3964e9dbad6fSeschrock } 3965e9dbad6fSeschrock } 3966e9dbad6fSeschrock 3967e9dbad6fSeschrock /* 3968e9dbad6fSeschrock * Now go through and check the width of any non-fixed columns 3969e9dbad6fSeschrock */ 3970e9dbad6fSeschrock for (entry = *plp; entry != NULL; entry = entry->pl_next) { 397143d68d68SYuri Pankov if (entry->pl_fixed && !literal) 3972e9dbad6fSeschrock continue; 3973e9dbad6fSeschrock 3974990b4856Slling if (entry->pl_prop != ZPROP_INVAL) { 3975e9dbad6fSeschrock if (zfs_prop_get(zhp, entry->pl_prop, 397643d68d68SYuri Pankov buf, sizeof (buf), NULL, NULL, 0, literal) == 0) { 3977e9dbad6fSeschrock if (strlen(buf) > entry->pl_width) 3978e9dbad6fSeschrock entry->pl_width = strlen(buf); 3979e9dbad6fSeschrock } 398092241e0bSTom Erickson if (received && zfs_prop_get_recvd(zhp, 398192241e0bSTom Erickson zfs_prop_to_name(entry->pl_prop), 398243d68d68SYuri Pankov buf, sizeof (buf), literal) == 0) 398392241e0bSTom Erickson if (strlen(buf) > entry->pl_recvd_width) 398492241e0bSTom Erickson entry->pl_recvd_width = strlen(buf); 398592241e0bSTom Erickson } else { 398692241e0bSTom Erickson if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop, 398792241e0bSTom Erickson &propval) == 0) { 3988e9dbad6fSeschrock verify(nvlist_lookup_string(propval, 3989990b4856Slling ZPROP_VALUE, &strval) == 0); 3990e9dbad6fSeschrock if (strlen(strval) > entry->pl_width) 3991e9dbad6fSeschrock entry->pl_width = strlen(strval); 3992e9dbad6fSeschrock } 399392241e0bSTom Erickson if (received && zfs_prop_get_recvd(zhp, 399492241e0bSTom Erickson entry->pl_user_prop, 399543d68d68SYuri Pankov buf, sizeof (buf), literal) == 0) 399692241e0bSTom Erickson if (strlen(buf) > entry->pl_recvd_width) 399792241e0bSTom Erickson entry->pl_recvd_width = strlen(buf); 399892241e0bSTom Erickson } 3999e9dbad6fSeschrock } 4000e9dbad6fSeschrock 4001e9dbad6fSeschrock return (0); 4002e9dbad6fSeschrock } 4003ecd6cf80Smarks 4004ecd6cf80Smarks int 4005ecd6cf80Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path, 4006743a77edSAlan Wright char *resource, void *export, void *sharetab, 4007743a77edSAlan Wright int sharemax, zfs_share_op_t operation) 4008ecd6cf80Smarks { 4009ecd6cf80Smarks zfs_cmd_t zc = { 0 }; 4010ecd6cf80Smarks int error; 4011ecd6cf80Smarks 4012ecd6cf80Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 4013ecd6cf80Smarks (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value)); 4014743a77edSAlan Wright if (resource) 4015743a77edSAlan Wright (void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string)); 4016ecd6cf80Smarks zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab; 4017ecd6cf80Smarks zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export; 4018da6c28aaSamw zc.zc_share.z_sharetype = operation; 4019ecd6cf80Smarks zc.zc_share.z_sharemax = sharemax; 4020ecd6cf80Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc); 4021ecd6cf80Smarks return (error); 4022ecd6cf80Smarks } 40232e5e9e19SSanjeev Bagewadi 40242e5e9e19SSanjeev Bagewadi void 40252e5e9e19SSanjeev Bagewadi zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props) 40262e5e9e19SSanjeev Bagewadi { 40272e5e9e19SSanjeev Bagewadi nvpair_t *curr; 40282e5e9e19SSanjeev Bagewadi 40292e5e9e19SSanjeev Bagewadi /* 40302e5e9e19SSanjeev Bagewadi * Keep a reference to the props-table against which we prune the 40312e5e9e19SSanjeev Bagewadi * properties. 40322e5e9e19SSanjeev Bagewadi */ 40332e5e9e19SSanjeev Bagewadi zhp->zfs_props_table = props; 40342e5e9e19SSanjeev Bagewadi 40352e5e9e19SSanjeev Bagewadi curr = nvlist_next_nvpair(zhp->zfs_props, NULL); 40362e5e9e19SSanjeev Bagewadi 40372e5e9e19SSanjeev Bagewadi while (curr) { 40382e5e9e19SSanjeev Bagewadi zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr)); 40392e5e9e19SSanjeev Bagewadi nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr); 40402e5e9e19SSanjeev Bagewadi 404114843421SMatthew Ahrens /* 4042faaa6415SEric Schrock * User properties will result in ZPROP_INVAL, and since we 4043faaa6415SEric Schrock * only know how to prune standard ZFS properties, we always 4044faaa6415SEric Schrock * leave these in the list. This can also happen if we 4045faaa6415SEric Schrock * encounter an unknown DSL property (when running older 4046faaa6415SEric Schrock * software, for example). 404714843421SMatthew Ahrens */ 404814843421SMatthew Ahrens if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE) 40492e5e9e19SSanjeev Bagewadi (void) nvlist_remove(zhp->zfs_props, 40502e5e9e19SSanjeev Bagewadi nvpair_name(curr), nvpair_type(curr)); 40512e5e9e19SSanjeev Bagewadi curr = next; 40522e5e9e19SSanjeev Bagewadi } 40532e5e9e19SSanjeev Bagewadi } 4054743a77edSAlan Wright 4055743a77edSAlan Wright static int 4056743a77edSAlan Wright zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path, 4057743a77edSAlan Wright zfs_smb_acl_op_t cmd, char *resource1, char *resource2) 4058743a77edSAlan Wright { 4059743a77edSAlan Wright zfs_cmd_t zc = { 0 }; 4060743a77edSAlan Wright nvlist_t *nvlist = NULL; 4061743a77edSAlan Wright int error; 4062743a77edSAlan Wright 4063743a77edSAlan Wright (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 4064743a77edSAlan Wright (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value)); 4065743a77edSAlan Wright zc.zc_cookie = (uint64_t)cmd; 4066743a77edSAlan Wright 4067743a77edSAlan Wright if (cmd == ZFS_SMB_ACL_RENAME) { 4068743a77edSAlan Wright if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) { 4069743a77edSAlan Wright (void) no_memory(hdl); 4070743a77edSAlan Wright return (NULL); 4071743a77edSAlan Wright } 4072743a77edSAlan Wright } 4073743a77edSAlan Wright 4074743a77edSAlan Wright switch (cmd) { 4075743a77edSAlan Wright case ZFS_SMB_ACL_ADD: 4076743a77edSAlan Wright case ZFS_SMB_ACL_REMOVE: 4077743a77edSAlan Wright (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string)); 4078743a77edSAlan Wright break; 4079743a77edSAlan Wright case ZFS_SMB_ACL_RENAME: 4080743a77edSAlan Wright if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC, 4081743a77edSAlan Wright resource1) != 0) { 4082743a77edSAlan Wright (void) no_memory(hdl); 4083743a77edSAlan Wright return (-1); 4084743a77edSAlan Wright } 4085743a77edSAlan Wright if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET, 4086743a77edSAlan Wright resource2) != 0) { 4087743a77edSAlan Wright (void) no_memory(hdl); 4088743a77edSAlan Wright return (-1); 4089743a77edSAlan Wright } 4090743a77edSAlan Wright if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) { 4091743a77edSAlan Wright nvlist_free(nvlist); 4092743a77edSAlan Wright return (-1); 4093743a77edSAlan Wright } 4094743a77edSAlan Wright break; 4095743a77edSAlan Wright case ZFS_SMB_ACL_PURGE: 4096743a77edSAlan Wright break; 4097743a77edSAlan Wright default: 4098743a77edSAlan Wright return (-1); 4099743a77edSAlan Wright } 4100743a77edSAlan Wright error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc); 4101743a77edSAlan Wright if (nvlist) 4102743a77edSAlan Wright nvlist_free(nvlist); 4103743a77edSAlan Wright return (error); 4104743a77edSAlan Wright } 4105743a77edSAlan Wright 4106743a77edSAlan Wright int 4107743a77edSAlan Wright zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset, 4108743a77edSAlan Wright char *path, char *resource) 4109743a77edSAlan Wright { 4110743a77edSAlan Wright return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD, 4111743a77edSAlan Wright resource, NULL)); 4112743a77edSAlan Wright } 4113743a77edSAlan Wright 4114743a77edSAlan Wright int 4115743a77edSAlan Wright zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset, 4116743a77edSAlan Wright char *path, char *resource) 4117743a77edSAlan Wright { 4118743a77edSAlan Wright return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE, 4119743a77edSAlan Wright resource, NULL)); 4120743a77edSAlan Wright } 4121743a77edSAlan Wright 4122743a77edSAlan Wright int 4123743a77edSAlan Wright zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path) 4124743a77edSAlan Wright { 4125743a77edSAlan Wright return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE, 4126743a77edSAlan Wright NULL, NULL)); 4127743a77edSAlan Wright } 4128743a77edSAlan Wright 4129743a77edSAlan Wright int 4130743a77edSAlan Wright zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path, 4131743a77edSAlan Wright char *oldname, char *newname) 4132743a77edSAlan Wright { 4133743a77edSAlan Wright return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME, 4134743a77edSAlan Wright oldname, newname)); 4135743a77edSAlan Wright } 413614843421SMatthew Ahrens 413714843421SMatthew Ahrens int 413814843421SMatthew Ahrens zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type, 413914843421SMatthew Ahrens zfs_userspace_cb_t func, void *arg) 414014843421SMatthew Ahrens { 414114843421SMatthew Ahrens zfs_cmd_t zc = { 0 }; 414214843421SMatthew Ahrens zfs_useracct_t buf[100]; 414370f56fa6SYuri Pankov libzfs_handle_t *hdl = zhp->zfs_hdl; 414470f56fa6SYuri Pankov int ret; 414514843421SMatthew Ahrens 414619b94df9SMatthew Ahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 414714843421SMatthew Ahrens 414814843421SMatthew Ahrens zc.zc_objset_type = type; 414914843421SMatthew Ahrens zc.zc_nvlist_dst = (uintptr_t)buf; 415014843421SMatthew Ahrens 415170f56fa6SYuri Pankov for (;;) { 415214843421SMatthew Ahrens zfs_useracct_t *zua = buf; 415314843421SMatthew Ahrens 415414843421SMatthew Ahrens zc.zc_nvlist_dst_size = sizeof (buf); 415570f56fa6SYuri Pankov if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) { 41563b2aab18SMatthew Ahrens char errbuf[1024]; 415770f56fa6SYuri Pankov 415870f56fa6SYuri Pankov (void) snprintf(errbuf, sizeof (errbuf), 415970f56fa6SYuri Pankov dgettext(TEXT_DOMAIN, 416070f56fa6SYuri Pankov "cannot get used/quota for %s"), zc.zc_name); 416170f56fa6SYuri Pankov return (zfs_standard_error_fmt(hdl, errno, errbuf)); 416270f56fa6SYuri Pankov } 416370f56fa6SYuri Pankov if (zc.zc_nvlist_dst_size == 0) 416414843421SMatthew Ahrens break; 416514843421SMatthew Ahrens 416614843421SMatthew Ahrens while (zc.zc_nvlist_dst_size > 0) { 416770f56fa6SYuri Pankov if ((ret = func(arg, zua->zu_domain, zua->zu_rid, 416870f56fa6SYuri Pankov zua->zu_space)) != 0) 416970f56fa6SYuri Pankov return (ret); 417014843421SMatthew Ahrens zua++; 417114843421SMatthew Ahrens zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t); 417214843421SMatthew Ahrens } 417314843421SMatthew Ahrens } 417414843421SMatthew Ahrens 417570f56fa6SYuri Pankov return (0); 417614843421SMatthew Ahrens } 4177842727c2SChris Kirby 41783b2aab18SMatthew Ahrens struct holdarg { 41793b2aab18SMatthew Ahrens nvlist_t *nvl; 41803b2aab18SMatthew Ahrens const char *snapname; 41813b2aab18SMatthew Ahrens const char *tag; 41823b2aab18SMatthew Ahrens boolean_t recursive; 4183bb6e7075SMatthew Ahrens int error; 41843b2aab18SMatthew Ahrens }; 41853b2aab18SMatthew Ahrens 41863b2aab18SMatthew Ahrens static int 41873b2aab18SMatthew Ahrens zfs_hold_one(zfs_handle_t *zhp, void *arg) 41883b2aab18SMatthew Ahrens { 41893b2aab18SMatthew Ahrens struct holdarg *ha = arg; 41903b2aab18SMatthew Ahrens char name[ZFS_MAXNAMELEN]; 41913b2aab18SMatthew Ahrens int rv = 0; 41923b2aab18SMatthew Ahrens 41933b2aab18SMatthew Ahrens (void) snprintf(name, sizeof (name), 41943b2aab18SMatthew Ahrens "%s@%s", zhp->zfs_name, ha->snapname); 41953b2aab18SMatthew Ahrens 4196a7a845e4SSteven Hartland if (lzc_exists(name)) 41973b2aab18SMatthew Ahrens fnvlist_add_string(ha->nvl, name, ha->tag); 41983b2aab18SMatthew Ahrens 41993b2aab18SMatthew Ahrens if (ha->recursive) 42003b2aab18SMatthew Ahrens rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha); 42013b2aab18SMatthew Ahrens zfs_close(zhp); 42023b2aab18SMatthew Ahrens return (rv); 42033b2aab18SMatthew Ahrens } 42043b2aab18SMatthew Ahrens 4205842727c2SChris Kirby int 4206842727c2SChris Kirby zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag, 4207a7a845e4SSteven Hartland boolean_t recursive, int cleanup_fd) 4208842727c2SChris Kirby { 42093b2aab18SMatthew Ahrens int ret; 42103b2aab18SMatthew Ahrens struct holdarg ha; 4211842727c2SChris Kirby 42123b2aab18SMatthew Ahrens ha.nvl = fnvlist_alloc(); 42133b2aab18SMatthew Ahrens ha.snapname = snapname; 42143b2aab18SMatthew Ahrens ha.tag = tag; 42153b2aab18SMatthew Ahrens ha.recursive = recursive; 42163b2aab18SMatthew Ahrens (void) zfs_hold_one(zfs_handle_dup(zhp), &ha); 4217013023d4SMartin Matuska 4218a7a845e4SSteven Hartland if (nvlist_empty(ha.nvl)) { 4219a7a845e4SSteven Hartland char errbuf[1024]; 4220a7a845e4SSteven Hartland 4221013023d4SMartin Matuska fnvlist_free(ha.nvl); 4222013023d4SMartin Matuska ret = ENOENT; 4223013023d4SMartin Matuska (void) snprintf(errbuf, sizeof (errbuf), 4224013023d4SMartin Matuska dgettext(TEXT_DOMAIN, 4225013023d4SMartin Matuska "cannot hold snapshot '%s@%s'"), 4226013023d4SMartin Matuska zhp->zfs_name, snapname); 4227a7a845e4SSteven Hartland (void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf); 4228013023d4SMartin Matuska return (ret); 4229013023d4SMartin Matuska } 4230013023d4SMartin Matuska 4231a7a845e4SSteven Hartland ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl); 42323b2aab18SMatthew Ahrens fnvlist_free(ha.nvl); 4233a7f53a56SChris Kirby 4234a7a845e4SSteven Hartland return (ret); 4235a7a845e4SSteven Hartland } 4236842727c2SChris Kirby 4237a7a845e4SSteven Hartland int 4238a7a845e4SSteven Hartland zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds) 4239a7a845e4SSteven Hartland { 4240a7a845e4SSteven Hartland int ret; 4241a7a845e4SSteven Hartland nvlist_t *errors; 4242a7a845e4SSteven Hartland libzfs_handle_t *hdl = zhp->zfs_hdl; 4243a7a845e4SSteven Hartland char errbuf[1024]; 4244a7a845e4SSteven Hartland nvpair_t *elem; 4245a7a845e4SSteven Hartland 4246a7a845e4SSteven Hartland errors = NULL; 4247a7a845e4SSteven Hartland ret = lzc_hold(holds, cleanup_fd, &errors); 4248a7a845e4SSteven Hartland 4249a7a845e4SSteven Hartland if (ret == 0) { 4250a7a845e4SSteven Hartland /* There may be errors even in the success case. */ 4251a7a845e4SSteven Hartland fnvlist_free(errors); 4252a7a845e4SSteven Hartland return (0); 4253a7a845e4SSteven Hartland } 4254a7a845e4SSteven Hartland 4255a7a845e4SSteven Hartland if (nvlist_empty(errors)) { 42563b2aab18SMatthew Ahrens /* no hold-specific errors */ 42573b2aab18SMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), 42583b2aab18SMatthew Ahrens dgettext(TEXT_DOMAIN, "cannot hold")); 42593b2aab18SMatthew Ahrens switch (ret) { 42603b2aab18SMatthew Ahrens case ENOTSUP: 42613b2aab18SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 42623b2aab18SMatthew Ahrens "pool must be upgraded")); 42633b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 42643b2aab18SMatthew Ahrens break; 42653b2aab18SMatthew Ahrens case EINVAL: 42663b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 42673b2aab18SMatthew Ahrens break; 42683b2aab18SMatthew Ahrens default: 42693b2aab18SMatthew Ahrens (void) zfs_standard_error(hdl, ret, errbuf); 42703b2aab18SMatthew Ahrens } 42713b2aab18SMatthew Ahrens } 4272842727c2SChris Kirby 42733b2aab18SMatthew Ahrens for (elem = nvlist_next_nvpair(errors, NULL); 42743b2aab18SMatthew Ahrens elem != NULL; 42753b2aab18SMatthew Ahrens elem = nvlist_next_nvpair(errors, elem)) { 42763b2aab18SMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), 42773b2aab18SMatthew Ahrens dgettext(TEXT_DOMAIN, 42783b2aab18SMatthew Ahrens "cannot hold snapshot '%s'"), nvpair_name(elem)); 42793b2aab18SMatthew Ahrens switch (fnvpair_value_int32(elem)) { 428015508ac0SChris Kirby case E2BIG: 428115508ac0SChris Kirby /* 428215508ac0SChris Kirby * Temporary tags wind up having the ds object id 428315508ac0SChris Kirby * prepended. So even if we passed the length check 428415508ac0SChris Kirby * above, it's still possible for the tag to wind 428515508ac0SChris Kirby * up being slightly too long. 428615508ac0SChris Kirby */ 42873b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf); 42883b2aab18SMatthew Ahrens break; 4289842727c2SChris Kirby case EINVAL: 42903b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 42913b2aab18SMatthew Ahrens break; 4292842727c2SChris Kirby case EEXIST: 42933b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf); 42943b2aab18SMatthew Ahrens break; 4295842727c2SChris Kirby default: 42963b2aab18SMatthew Ahrens (void) zfs_standard_error(hdl, 42973b2aab18SMatthew Ahrens fnvpair_value_int32(elem), errbuf); 4298842727c2SChris Kirby } 4299842727c2SChris Kirby } 4300842727c2SChris Kirby 43013b2aab18SMatthew Ahrens fnvlist_free(errors); 43023b2aab18SMatthew Ahrens return (ret); 43033b2aab18SMatthew Ahrens } 43043b2aab18SMatthew Ahrens 43053b2aab18SMatthew Ahrens static int 43063b2aab18SMatthew Ahrens zfs_release_one(zfs_handle_t *zhp, void *arg) 43073b2aab18SMatthew Ahrens { 43083b2aab18SMatthew Ahrens struct holdarg *ha = arg; 43093b2aab18SMatthew Ahrens char name[ZFS_MAXNAMELEN]; 43103b2aab18SMatthew Ahrens int rv = 0; 4311bb6e7075SMatthew Ahrens nvlist_t *existing_holds; 43123b2aab18SMatthew Ahrens 43133b2aab18SMatthew Ahrens (void) snprintf(name, sizeof (name), 43143b2aab18SMatthew Ahrens "%s@%s", zhp->zfs_name, ha->snapname); 43153b2aab18SMatthew Ahrens 4316bb6e7075SMatthew Ahrens if (lzc_get_holds(name, &existing_holds) != 0) { 4317bb6e7075SMatthew Ahrens ha->error = ENOENT; 4318bb6e7075SMatthew Ahrens } else if (!nvlist_exists(existing_holds, ha->tag)) { 4319bb6e7075SMatthew Ahrens ha->error = ESRCH; 4320bb6e7075SMatthew Ahrens } else { 4321bb6e7075SMatthew Ahrens nvlist_t *torelease = fnvlist_alloc(); 4322bb6e7075SMatthew Ahrens fnvlist_add_boolean(torelease, ha->tag); 4323bb6e7075SMatthew Ahrens fnvlist_add_nvlist(ha->nvl, name, torelease); 4324bb6e7075SMatthew Ahrens fnvlist_free(torelease); 43253b2aab18SMatthew Ahrens } 43263b2aab18SMatthew Ahrens 43273b2aab18SMatthew Ahrens if (ha->recursive) 43283b2aab18SMatthew Ahrens rv = zfs_iter_filesystems(zhp, zfs_release_one, ha); 43293b2aab18SMatthew Ahrens zfs_close(zhp); 43303b2aab18SMatthew Ahrens return (rv); 4331842727c2SChris Kirby } 4332842727c2SChris Kirby 4333842727c2SChris Kirby int 4334842727c2SChris Kirby zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag, 4335842727c2SChris Kirby boolean_t recursive) 4336842727c2SChris Kirby { 43373b2aab18SMatthew Ahrens int ret; 43383b2aab18SMatthew Ahrens struct holdarg ha; 4339a7a845e4SSteven Hartland nvlist_t *errors = NULL; 43403b2aab18SMatthew Ahrens nvpair_t *elem; 4341842727c2SChris Kirby libzfs_handle_t *hdl = zhp->zfs_hdl; 4342013023d4SMartin Matuska char errbuf[1024]; 4343842727c2SChris Kirby 43443b2aab18SMatthew Ahrens ha.nvl = fnvlist_alloc(); 43453b2aab18SMatthew Ahrens ha.snapname = snapname; 43463b2aab18SMatthew Ahrens ha.tag = tag; 43473b2aab18SMatthew Ahrens ha.recursive = recursive; 4348bb6e7075SMatthew Ahrens ha.error = 0; 43493b2aab18SMatthew Ahrens (void) zfs_release_one(zfs_handle_dup(zhp), &ha); 4350013023d4SMartin Matuska 4351a7a845e4SSteven Hartland if (nvlist_empty(ha.nvl)) { 4352013023d4SMartin Matuska fnvlist_free(ha.nvl); 4353bb6e7075SMatthew Ahrens ret = ha.error; 4354013023d4SMartin Matuska (void) snprintf(errbuf, sizeof (errbuf), 4355013023d4SMartin Matuska dgettext(TEXT_DOMAIN, 4356013023d4SMartin Matuska "cannot release hold from snapshot '%s@%s'"), 4357013023d4SMartin Matuska zhp->zfs_name, snapname); 4358bb6e7075SMatthew Ahrens if (ret == ESRCH) { 4359bb6e7075SMatthew Ahrens (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf); 4360bb6e7075SMatthew Ahrens } else { 4361013023d4SMartin Matuska (void) zfs_standard_error(hdl, ret, errbuf); 4362bb6e7075SMatthew Ahrens } 4363013023d4SMartin Matuska return (ret); 4364013023d4SMartin Matuska } 4365013023d4SMartin Matuska 43663b2aab18SMatthew Ahrens ret = lzc_release(ha.nvl, &errors); 43673b2aab18SMatthew Ahrens fnvlist_free(ha.nvl); 4368842727c2SChris Kirby 4369a7a845e4SSteven Hartland if (ret == 0) { 4370a7a845e4SSteven Hartland /* There may be errors even in the success case. */ 4371a7a845e4SSteven Hartland fnvlist_free(errors); 43723b2aab18SMatthew Ahrens return (0); 4373a7a845e4SSteven Hartland } 4374842727c2SChris Kirby 4375a7a845e4SSteven Hartland if (nvlist_empty(errors)) { 43763b2aab18SMatthew Ahrens /* no hold-specific errors */ 4377842727c2SChris Kirby (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 43783b2aab18SMatthew Ahrens "cannot release")); 4379842727c2SChris Kirby switch (errno) { 4380842727c2SChris Kirby case ENOTSUP: 4381842727c2SChris Kirby zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4382842727c2SChris Kirby "pool must be upgraded")); 43833b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 43843b2aab18SMatthew Ahrens break; 4385842727c2SChris Kirby default: 43863b2aab18SMatthew Ahrens (void) zfs_standard_error_fmt(hdl, errno, errbuf); 4387842727c2SChris Kirby } 4388842727c2SChris Kirby } 4389842727c2SChris Kirby 43903b2aab18SMatthew Ahrens for (elem = nvlist_next_nvpair(errors, NULL); 43913b2aab18SMatthew Ahrens elem != NULL; 43923b2aab18SMatthew Ahrens elem = nvlist_next_nvpair(errors, elem)) { 43933b2aab18SMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), 43943b2aab18SMatthew Ahrens dgettext(TEXT_DOMAIN, 43953b2aab18SMatthew Ahrens "cannot release hold from snapshot '%s'"), 43963b2aab18SMatthew Ahrens nvpair_name(elem)); 43973b2aab18SMatthew Ahrens switch (fnvpair_value_int32(elem)) { 43983b2aab18SMatthew Ahrens case ESRCH: 43993b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf); 44003b2aab18SMatthew Ahrens break; 44013b2aab18SMatthew Ahrens case EINVAL: 44023b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 44033b2aab18SMatthew Ahrens break; 44043b2aab18SMatthew Ahrens default: 44053b2aab18SMatthew Ahrens (void) zfs_standard_error_fmt(hdl, 44063b2aab18SMatthew Ahrens fnvpair_value_int32(elem), errbuf); 44073b2aab18SMatthew Ahrens } 44083b2aab18SMatthew Ahrens } 44093b2aab18SMatthew Ahrens 44103b2aab18SMatthew Ahrens fnvlist_free(errors); 44113b2aab18SMatthew Ahrens return (ret); 4412842727c2SChris Kirby } 4413ca45db41SChris Kirby 44141af68beaSAlexander Stetsenko int 44151af68beaSAlexander Stetsenko zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl) 44161af68beaSAlexander Stetsenko { 44171af68beaSAlexander Stetsenko zfs_cmd_t zc = { 0 }; 44181af68beaSAlexander Stetsenko libzfs_handle_t *hdl = zhp->zfs_hdl; 44191af68beaSAlexander Stetsenko int nvsz = 2048; 44201af68beaSAlexander Stetsenko void *nvbuf; 44211af68beaSAlexander Stetsenko int err = 0; 44223b2aab18SMatthew Ahrens char errbuf[1024]; 44231af68beaSAlexander Stetsenko 44241af68beaSAlexander Stetsenko assert(zhp->zfs_type == ZFS_TYPE_VOLUME || 44251af68beaSAlexander Stetsenko zhp->zfs_type == ZFS_TYPE_FILESYSTEM); 44261af68beaSAlexander Stetsenko 44271af68beaSAlexander Stetsenko tryagain: 44281af68beaSAlexander Stetsenko 44291af68beaSAlexander Stetsenko nvbuf = malloc(nvsz); 44301af68beaSAlexander Stetsenko if (nvbuf == NULL) { 44311af68beaSAlexander Stetsenko err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno))); 44321af68beaSAlexander Stetsenko goto out; 44331af68beaSAlexander Stetsenko } 44341af68beaSAlexander Stetsenko 44351af68beaSAlexander Stetsenko zc.zc_nvlist_dst_size = nvsz; 44361af68beaSAlexander Stetsenko zc.zc_nvlist_dst = (uintptr_t)nvbuf; 44371af68beaSAlexander Stetsenko 44381af68beaSAlexander Stetsenko (void) strlcpy(zc.zc_name, zhp->zfs_name, ZFS_MAXNAMELEN); 44391af68beaSAlexander Stetsenko 4440d7f601efSGeorge Wilson if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) { 44411af68beaSAlexander Stetsenko (void) snprintf(errbuf, sizeof (errbuf), 44421af68beaSAlexander Stetsenko dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"), 44431af68beaSAlexander Stetsenko zc.zc_name); 44441af68beaSAlexander Stetsenko switch (errno) { 44451af68beaSAlexander Stetsenko case ENOMEM: 44461af68beaSAlexander Stetsenko free(nvbuf); 44471af68beaSAlexander Stetsenko nvsz = zc.zc_nvlist_dst_size; 44481af68beaSAlexander Stetsenko goto tryagain; 44491af68beaSAlexander Stetsenko 44501af68beaSAlexander Stetsenko case ENOTSUP: 44511af68beaSAlexander Stetsenko zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 44521af68beaSAlexander Stetsenko "pool must be upgraded")); 44531af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADVERSION, errbuf); 44541af68beaSAlexander Stetsenko break; 44551af68beaSAlexander Stetsenko case EINVAL: 44561af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADTYPE, errbuf); 44571af68beaSAlexander Stetsenko break; 44581af68beaSAlexander Stetsenko case ENOENT: 44591af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_NOENT, errbuf); 44601af68beaSAlexander Stetsenko break; 44611af68beaSAlexander Stetsenko default: 44621af68beaSAlexander Stetsenko err = zfs_standard_error_fmt(hdl, errno, errbuf); 44631af68beaSAlexander Stetsenko break; 44641af68beaSAlexander Stetsenko } 44651af68beaSAlexander Stetsenko } else { 44661af68beaSAlexander Stetsenko /* success */ 44671af68beaSAlexander Stetsenko int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0); 44681af68beaSAlexander Stetsenko if (rc) { 44691af68beaSAlexander Stetsenko (void) snprintf(errbuf, sizeof (errbuf), dgettext( 44701af68beaSAlexander Stetsenko TEXT_DOMAIN, "cannot get permissions on '%s'"), 44711af68beaSAlexander Stetsenko zc.zc_name); 44721af68beaSAlexander Stetsenko err = zfs_standard_error_fmt(hdl, rc, errbuf); 44731af68beaSAlexander Stetsenko } 44741af68beaSAlexander Stetsenko } 44751af68beaSAlexander Stetsenko 44761af68beaSAlexander Stetsenko free(nvbuf); 44771af68beaSAlexander Stetsenko out: 44781af68beaSAlexander Stetsenko return (err); 44791af68beaSAlexander Stetsenko } 44801af68beaSAlexander Stetsenko 44811af68beaSAlexander Stetsenko int 44821af68beaSAlexander Stetsenko zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl) 44831af68beaSAlexander Stetsenko { 44841af68beaSAlexander Stetsenko zfs_cmd_t zc = { 0 }; 44851af68beaSAlexander Stetsenko libzfs_handle_t *hdl = zhp->zfs_hdl; 44861af68beaSAlexander Stetsenko char *nvbuf; 44873b2aab18SMatthew Ahrens char errbuf[1024]; 44881af68beaSAlexander Stetsenko size_t nvsz; 44891af68beaSAlexander Stetsenko int err; 44901af68beaSAlexander Stetsenko 44911af68beaSAlexander Stetsenko assert(zhp->zfs_type == ZFS_TYPE_VOLUME || 44921af68beaSAlexander Stetsenko zhp->zfs_type == ZFS_TYPE_FILESYSTEM); 44931af68beaSAlexander Stetsenko 44941af68beaSAlexander Stetsenko err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE); 44951af68beaSAlexander Stetsenko assert(err == 0); 44961af68beaSAlexander Stetsenko 44971af68beaSAlexander Stetsenko nvbuf = malloc(nvsz); 44981af68beaSAlexander Stetsenko 44991af68beaSAlexander Stetsenko err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0); 45001af68beaSAlexander Stetsenko assert(err == 0); 45011af68beaSAlexander Stetsenko 45021af68beaSAlexander Stetsenko zc.zc_nvlist_src_size = nvsz; 45031af68beaSAlexander Stetsenko zc.zc_nvlist_src = (uintptr_t)nvbuf; 45041af68beaSAlexander Stetsenko zc.zc_perm_action = un; 45051af68beaSAlexander Stetsenko 45061af68beaSAlexander Stetsenko (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 45071af68beaSAlexander Stetsenko 45081af68beaSAlexander Stetsenko if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) { 45091af68beaSAlexander Stetsenko (void) snprintf(errbuf, sizeof (errbuf), 45101af68beaSAlexander Stetsenko dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"), 45111af68beaSAlexander Stetsenko zc.zc_name); 45121af68beaSAlexander Stetsenko switch (errno) { 45131af68beaSAlexander Stetsenko case ENOTSUP: 45141af68beaSAlexander Stetsenko zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 45151af68beaSAlexander Stetsenko "pool must be upgraded")); 45161af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADVERSION, errbuf); 45171af68beaSAlexander Stetsenko break; 45181af68beaSAlexander Stetsenko case EINVAL: 45191af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADTYPE, errbuf); 45201af68beaSAlexander Stetsenko break; 45211af68beaSAlexander Stetsenko case ENOENT: 45221af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_NOENT, errbuf); 45231af68beaSAlexander Stetsenko break; 45241af68beaSAlexander Stetsenko default: 45251af68beaSAlexander Stetsenko err = zfs_standard_error_fmt(hdl, errno, errbuf); 45261af68beaSAlexander Stetsenko break; 45271af68beaSAlexander Stetsenko } 45281af68beaSAlexander Stetsenko } 45291af68beaSAlexander Stetsenko 45301af68beaSAlexander Stetsenko free(nvbuf); 45311af68beaSAlexander Stetsenko 45321af68beaSAlexander Stetsenko return (err); 45331af68beaSAlexander Stetsenko } 45341af68beaSAlexander Stetsenko 45351af68beaSAlexander Stetsenko int 45361af68beaSAlexander Stetsenko zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl) 45371af68beaSAlexander Stetsenko { 45383b2aab18SMatthew Ahrens int err; 45393b2aab18SMatthew Ahrens char errbuf[1024]; 45403b2aab18SMatthew Ahrens 45413b2aab18SMatthew Ahrens err = lzc_get_holds(zhp->zfs_name, nvl); 45423b2aab18SMatthew Ahrens 45433b2aab18SMatthew Ahrens if (err != 0) { 45441af68beaSAlexander Stetsenko libzfs_handle_t *hdl = zhp->zfs_hdl; 45451af68beaSAlexander Stetsenko 45461af68beaSAlexander Stetsenko (void) snprintf(errbuf, sizeof (errbuf), 45471af68beaSAlexander Stetsenko dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"), 45483b2aab18SMatthew Ahrens zhp->zfs_name); 45493b2aab18SMatthew Ahrens switch (err) { 45501af68beaSAlexander Stetsenko case ENOTSUP: 45511af68beaSAlexander Stetsenko zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 45521af68beaSAlexander Stetsenko "pool must be upgraded")); 45531af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADVERSION, errbuf); 45541af68beaSAlexander Stetsenko break; 45551af68beaSAlexander Stetsenko case EINVAL: 45561af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADTYPE, errbuf); 45571af68beaSAlexander Stetsenko break; 45581af68beaSAlexander Stetsenko case ENOENT: 45591af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_NOENT, errbuf); 45601af68beaSAlexander Stetsenko break; 45611af68beaSAlexander Stetsenko default: 45621af68beaSAlexander Stetsenko err = zfs_standard_error_fmt(hdl, errno, errbuf); 45631af68beaSAlexander Stetsenko break; 45641af68beaSAlexander Stetsenko } 45651af68beaSAlexander Stetsenko } 45661af68beaSAlexander Stetsenko 45671af68beaSAlexander Stetsenko return (err); 45681af68beaSAlexander Stetsenko } 45691af68beaSAlexander Stetsenko 45703e30c24aSWill Andrews /* 45713e30c24aSWill Andrews * Convert the zvol's volume size to an appropriate reservation. 45723e30c24aSWill Andrews * Note: If this routine is updated, it is necessary to update the ZFS test 45733e30c24aSWill Andrews * suite's shell version in reservation.kshlib. 45743e30c24aSWill Andrews */ 4575c1449561SEric Taylor uint64_t 4576c1449561SEric Taylor zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props) 4577c1449561SEric Taylor { 4578c1449561SEric Taylor uint64_t numdb; 4579c1449561SEric Taylor uint64_t nblocks, volblocksize; 4580c1449561SEric Taylor int ncopies; 4581c1449561SEric Taylor char *strval; 4582c1449561SEric Taylor 4583c1449561SEric Taylor if (nvlist_lookup_string(props, 4584c1449561SEric Taylor zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0) 4585c1449561SEric Taylor ncopies = atoi(strval); 4586c1449561SEric Taylor else 4587c1449561SEric Taylor ncopies = 1; 4588c1449561SEric Taylor if (nvlist_lookup_uint64(props, 4589c1449561SEric Taylor zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 4590c1449561SEric Taylor &volblocksize) != 0) 4591c1449561SEric Taylor volblocksize = ZVOL_DEFAULT_BLOCKSIZE; 4592c1449561SEric Taylor nblocks = volsize/volblocksize; 4593c1449561SEric Taylor /* start with metadnode L0-L6 */ 4594c1449561SEric Taylor numdb = 7; 4595c1449561SEric Taylor /* calculate number of indirects */ 4596c1449561SEric Taylor while (nblocks > 1) { 4597c1449561SEric Taylor nblocks += DNODES_PER_LEVEL - 1; 4598c1449561SEric Taylor nblocks /= DNODES_PER_LEVEL; 4599c1449561SEric Taylor numdb += nblocks; 4600c1449561SEric Taylor } 4601c1449561SEric Taylor numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1); 4602c1449561SEric Taylor volsize *= ncopies; 4603c1449561SEric Taylor /* 4604c1449561SEric Taylor * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't 4605c1449561SEric Taylor * compressed, but in practice they compress down to about 4606c1449561SEric Taylor * 1100 bytes 4607c1449561SEric Taylor */ 4608c1449561SEric Taylor numdb *= 1ULL << DN_MAX_INDBLKSHIFT; 4609c1449561SEric Taylor volsize += numdb; 4610c1449561SEric Taylor return (volsize); 4611c1449561SEric Taylor } 4612