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. 25ad2760acSMatthew Ahrens * Copyright (c) 2011, 2016 by Delphix. All rights reserved. 26f0f3ef5aSGarrett D'Amore * Copyright (c) 2012 DEY Storage Systems, Inc. All rights reserved. 270d8fa8f8SMartin Matuska * Copyright (c) 2011-2012 Pawel Jakub Dawidek. All rights reserved. 28013023d4SMartin Matuska * Copyright (c) 2013 Martin Matuska. All rights reserved. 29a7a845e4SSteven Hartland * Copyright (c) 2013 Steven Hartland. All rights reserved. 30c3d26abcSMatthew Ahrens * Copyright (c) 2014 Integros [integros.com] 318808ac5dSYuri Pankov * Copyright 2016 Nexenta Systems, Inc. 3288f61deeSIgor Kozhukhov * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com> 33fa9e4066Sahrens */ 34fa9e4066Sahrens 35fa9e4066Sahrens #include <ctype.h> 36fa9e4066Sahrens #include <errno.h> 37fa9e4066Sahrens #include <libintl.h> 38fa9e4066Sahrens #include <math.h> 39fa9e4066Sahrens #include <stdio.h> 40fa9e4066Sahrens #include <stdlib.h> 41fa9e4066Sahrens #include <strings.h> 42fa9e4066Sahrens #include <unistd.h> 433cb34c60Sahrens #include <stddef.h> 44fa9e4066Sahrens #include <zone.h> 4599653d4eSeschrock #include <fcntl.h> 46fa9e4066Sahrens #include <sys/mntent.h> 47b12a1c38Slling #include <sys/mount.h> 48ecd6cf80Smarks #include <priv.h> 49ecd6cf80Smarks #include <pwd.h> 50ecd6cf80Smarks #include <grp.h> 51ecd6cf80Smarks #include <stddef.h> 52ecd6cf80Smarks #include <ucred.h> 5314843421SMatthew Ahrens #include <idmap.h> 5414843421SMatthew Ahrens #include <aclutils.h> 553b12c289SMatthew Ahrens #include <directory.h> 56fa9e4066Sahrens 57c1449561SEric Taylor #include <sys/dnode.h> 58fa9e4066Sahrens #include <sys/spa.h> 59e9dbad6fSeschrock #include <sys/zap.h> 60fa9e4066Sahrens #include <libzfs.h> 61fa9e4066Sahrens 62fa9e4066Sahrens #include "zfs_namecheck.h" 63fa9e4066Sahrens #include "zfs_prop.h" 64fa9e4066Sahrens #include "libzfs_impl.h" 65ecd6cf80Smarks #include "zfs_deleg.h" 66fa9e4066Sahrens 6714843421SMatthew Ahrens static int userquota_propname_decode(const char *propname, boolean_t zoned, 6814843421SMatthew Ahrens zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp); 69cdf5b4caSmmusante 70fa9e4066Sahrens /* 71fa9e4066Sahrens * Given a single type (not a mask of types), return the type in a human 72fa9e4066Sahrens * readable form. 73fa9e4066Sahrens */ 74fa9e4066Sahrens const char * 75fa9e4066Sahrens zfs_type_to_name(zfs_type_t type) 76fa9e4066Sahrens { 77fa9e4066Sahrens switch (type) { 78fa9e4066Sahrens case ZFS_TYPE_FILESYSTEM: 79fa9e4066Sahrens return (dgettext(TEXT_DOMAIN, "filesystem")); 80fa9e4066Sahrens case ZFS_TYPE_SNAPSHOT: 81fa9e4066Sahrens return (dgettext(TEXT_DOMAIN, "snapshot")); 82fa9e4066Sahrens case ZFS_TYPE_VOLUME: 83fa9e4066Sahrens return (dgettext(TEXT_DOMAIN, "volume")); 8488f61deeSIgor Kozhukhov case ZFS_TYPE_POOL: 8588f61deeSIgor Kozhukhov return (dgettext(TEXT_DOMAIN, "pool")); 8688f61deeSIgor Kozhukhov case ZFS_TYPE_BOOKMARK: 8788f61deeSIgor Kozhukhov return (dgettext(TEXT_DOMAIN, "bookmark")); 8888f61deeSIgor Kozhukhov default: 8988f61deeSIgor Kozhukhov assert(!"unhandled zfs_type_t"); 90fa9e4066Sahrens } 91fa9e4066Sahrens 92fa9e4066Sahrens return (NULL); 93fa9e4066Sahrens } 94fa9e4066Sahrens 95fa9e4066Sahrens /* 96fa9e4066Sahrens * Validate a ZFS path. This is used even before trying to open the dataset, to 9714843421SMatthew Ahrens * provide a more meaningful error message. We call zfs_error_aux() to 9814843421SMatthew Ahrens * explain exactly why the name was not valid. 99fa9e4066Sahrens */ 10099d5e173STim Haley int 101f18faf3fSek110237 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type, 102f18faf3fSek110237 boolean_t modifying) 103fa9e4066Sahrens { 104fa9e4066Sahrens namecheck_err_t why; 105fa9e4066Sahrens char what; 106fa9e4066Sahrens 1071af68beaSAlexander Stetsenko (void) zfs_prop_get_table(); 108edb901aaSMarcel Telka if (entity_namecheck(path, &why, &what) != 0) { 10999653d4eSeschrock if (hdl != NULL) { 110fa9e4066Sahrens switch (why) { 111b81d61a6Slling case NAME_ERR_TOOLONG: 11299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11399653d4eSeschrock "name is too long")); 114b81d61a6Slling break; 115b81d61a6Slling 116fa9e4066Sahrens case NAME_ERR_LEADING_SLASH: 11799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11899653d4eSeschrock "leading slash in name")); 119fa9e4066Sahrens break; 120fa9e4066Sahrens 121fa9e4066Sahrens case NAME_ERR_EMPTY_COMPONENT: 12299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 12399653d4eSeschrock "empty component in name")); 124fa9e4066Sahrens break; 125fa9e4066Sahrens 126fa9e4066Sahrens case NAME_ERR_TRAILING_SLASH: 12799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 12899653d4eSeschrock "trailing slash in name")); 129fa9e4066Sahrens break; 130fa9e4066Sahrens 131fa9e4066Sahrens case NAME_ERR_INVALCHAR: 13299653d4eSeschrock zfs_error_aux(hdl, 133fa9e4066Sahrens dgettext(TEXT_DOMAIN, "invalid character " 13499653d4eSeschrock "'%c' in name"), what); 135fa9e4066Sahrens break; 136fa9e4066Sahrens 137edb901aaSMarcel Telka case NAME_ERR_MULTIPLE_DELIMITERS: 13899653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 139edb901aaSMarcel Telka "multiple '@' and/or '#' delimiters in " 140edb901aaSMarcel Telka "name")); 141fa9e4066Sahrens break; 1425ad82045Snd150628 1435ad82045Snd150628 case NAME_ERR_NOLETTER: 1445ad82045Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1455ad82045Snd150628 "pool doesn't begin with a letter")); 1465ad82045Snd150628 break; 1475ad82045Snd150628 1485ad82045Snd150628 case NAME_ERR_RESERVED: 1495ad82045Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1505ad82045Snd150628 "name is reserved")); 1515ad82045Snd150628 break; 1525ad82045Snd150628 1535ad82045Snd150628 case NAME_ERR_DISKLIKE: 1545ad82045Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1555ad82045Snd150628 "reserved disk name")); 1565ad82045Snd150628 break; 15788f61deeSIgor Kozhukhov 15888f61deeSIgor Kozhukhov default: 15988f61deeSIgor Kozhukhov zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 16088f61deeSIgor Kozhukhov "(%d) not defined"), why); 16188f61deeSIgor Kozhukhov break; 162fa9e4066Sahrens } 163fa9e4066Sahrens } 164fa9e4066Sahrens 165fa9e4066Sahrens return (0); 166fa9e4066Sahrens } 167fa9e4066Sahrens 168fa9e4066Sahrens if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) { 16999653d4eSeschrock if (hdl != NULL) 17099653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 171edb901aaSMarcel Telka "snapshot delimiter '@' is not expected here")); 172fa9e4066Sahrens return (0); 173fa9e4066Sahrens } 174fa9e4066Sahrens 1751d452cf5Sahrens if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) { 1761d452cf5Sahrens if (hdl != NULL) 1771d452cf5Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 178d7d4af51Smmusante "missing '@' delimiter in snapshot name")); 1791d452cf5Sahrens return (0); 1801d452cf5Sahrens } 1811d452cf5Sahrens 182edb901aaSMarcel Telka if (!(type & ZFS_TYPE_BOOKMARK) && strchr(path, '#') != NULL) { 183edb901aaSMarcel Telka if (hdl != NULL) 184edb901aaSMarcel Telka zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 185edb901aaSMarcel Telka "bookmark delimiter '#' is not expected here")); 186edb901aaSMarcel Telka return (0); 187edb901aaSMarcel Telka } 188edb901aaSMarcel Telka 189edb901aaSMarcel Telka if (type == ZFS_TYPE_BOOKMARK && strchr(path, '#') == NULL) { 190edb901aaSMarcel Telka if (hdl != NULL) 191edb901aaSMarcel Telka zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 192edb901aaSMarcel Telka "missing '#' delimiter in bookmark name")); 193edb901aaSMarcel Telka return (0); 194edb901aaSMarcel Telka } 195edb901aaSMarcel Telka 196f18faf3fSek110237 if (modifying && strchr(path, '%') != NULL) { 197f18faf3fSek110237 if (hdl != NULL) 198f18faf3fSek110237 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 199f18faf3fSek110237 "invalid character %c in name"), '%'); 200f18faf3fSek110237 return (0); 201f18faf3fSek110237 } 202f18faf3fSek110237 20399653d4eSeschrock return (-1); 204fa9e4066Sahrens } 205fa9e4066Sahrens 206fa9e4066Sahrens int 207fa9e4066Sahrens zfs_name_valid(const char *name, zfs_type_t type) 208fa9e4066Sahrens { 209e7cbe64fSgw25295 if (type == ZFS_TYPE_POOL) 210e7cbe64fSgw25295 return (zpool_name_valid(NULL, B_FALSE, name)); 211f18faf3fSek110237 return (zfs_validate_name(NULL, name, type, B_FALSE)); 212fa9e4066Sahrens } 213fa9e4066Sahrens 214fa9e4066Sahrens /* 215e9dbad6fSeschrock * This function takes the raw DSL properties, and filters out the user-defined 216e9dbad6fSeschrock * properties into a separate nvlist. 217e9dbad6fSeschrock */ 218fac3008cSeschrock static nvlist_t * 219fac3008cSeschrock process_user_props(zfs_handle_t *zhp, nvlist_t *props) 220e9dbad6fSeschrock { 221e9dbad6fSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 222e9dbad6fSeschrock nvpair_t *elem; 223e9dbad6fSeschrock nvlist_t *propval; 224fac3008cSeschrock nvlist_t *nvl; 225e9dbad6fSeschrock 226fac3008cSeschrock if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) { 227fac3008cSeschrock (void) no_memory(hdl); 228fac3008cSeschrock return (NULL); 229fac3008cSeschrock } 230e9dbad6fSeschrock 231e9dbad6fSeschrock elem = NULL; 232fac3008cSeschrock while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 233e9dbad6fSeschrock if (!zfs_prop_user(nvpair_name(elem))) 234e9dbad6fSeschrock continue; 235e9dbad6fSeschrock 236e9dbad6fSeschrock verify(nvpair_value_nvlist(elem, &propval) == 0); 237fac3008cSeschrock if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) { 238fac3008cSeschrock nvlist_free(nvl); 239fac3008cSeschrock (void) no_memory(hdl); 240fac3008cSeschrock return (NULL); 241fac3008cSeschrock } 242e9dbad6fSeschrock } 243e9dbad6fSeschrock 244fac3008cSeschrock return (nvl); 245e9dbad6fSeschrock } 246e9dbad6fSeschrock 24729ab75c9Srm160521 static zpool_handle_t * 24829ab75c9Srm160521 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name) 24929ab75c9Srm160521 { 25029ab75c9Srm160521 libzfs_handle_t *hdl = zhp->zfs_hdl; 25129ab75c9Srm160521 zpool_handle_t *zph; 25229ab75c9Srm160521 25329ab75c9Srm160521 if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) { 25429ab75c9Srm160521 if (hdl->libzfs_pool_handles != NULL) 25529ab75c9Srm160521 zph->zpool_next = hdl->libzfs_pool_handles; 25629ab75c9Srm160521 hdl->libzfs_pool_handles = zph; 25729ab75c9Srm160521 } 25829ab75c9Srm160521 return (zph); 25929ab75c9Srm160521 } 26029ab75c9Srm160521 26129ab75c9Srm160521 static zpool_handle_t * 26229ab75c9Srm160521 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len) 26329ab75c9Srm160521 { 26429ab75c9Srm160521 libzfs_handle_t *hdl = zhp->zfs_hdl; 26529ab75c9Srm160521 zpool_handle_t *zph = hdl->libzfs_pool_handles; 26629ab75c9Srm160521 26729ab75c9Srm160521 while ((zph != NULL) && 26829ab75c9Srm160521 (strncmp(pool_name, zpool_get_name(zph), len) != 0)) 26929ab75c9Srm160521 zph = zph->zpool_next; 27029ab75c9Srm160521 return (zph); 27129ab75c9Srm160521 } 27229ab75c9Srm160521 27329ab75c9Srm160521 /* 27429ab75c9Srm160521 * Returns a handle to the pool that contains the provided dataset. 27529ab75c9Srm160521 * If a handle to that pool already exists then that handle is returned. 27629ab75c9Srm160521 * Otherwise, a new handle is created and added to the list of handles. 27729ab75c9Srm160521 */ 27829ab75c9Srm160521 static zpool_handle_t * 27929ab75c9Srm160521 zpool_handle(zfs_handle_t *zhp) 28029ab75c9Srm160521 { 28129ab75c9Srm160521 char *pool_name; 28229ab75c9Srm160521 int len; 28329ab75c9Srm160521 zpool_handle_t *zph; 28429ab75c9Srm160521 28578f17100SMatthew Ahrens len = strcspn(zhp->zfs_name, "/@#") + 1; 28629ab75c9Srm160521 pool_name = zfs_alloc(zhp->zfs_hdl, len); 28729ab75c9Srm160521 (void) strlcpy(pool_name, zhp->zfs_name, len); 28829ab75c9Srm160521 28929ab75c9Srm160521 zph = zpool_find_handle(zhp, pool_name, len); 29029ab75c9Srm160521 if (zph == NULL) 29129ab75c9Srm160521 zph = zpool_add_handle(zhp, pool_name); 29229ab75c9Srm160521 29329ab75c9Srm160521 free(pool_name); 29429ab75c9Srm160521 return (zph); 29529ab75c9Srm160521 } 29629ab75c9Srm160521 29729ab75c9Srm160521 void 29829ab75c9Srm160521 zpool_free_handles(libzfs_handle_t *hdl) 29929ab75c9Srm160521 { 30029ab75c9Srm160521 zpool_handle_t *next, *zph = hdl->libzfs_pool_handles; 30129ab75c9Srm160521 30229ab75c9Srm160521 while (zph != NULL) { 30329ab75c9Srm160521 next = zph->zpool_next; 30429ab75c9Srm160521 zpool_close(zph); 30529ab75c9Srm160521 zph = next; 30629ab75c9Srm160521 } 30729ab75c9Srm160521 hdl->libzfs_pool_handles = NULL; 30829ab75c9Srm160521 } 30929ab75c9Srm160521 310e9dbad6fSeschrock /* 311fa9e4066Sahrens * Utility function to gather stats (objset and zpl) for the given object. 312fa9e4066Sahrens */ 313fa9e4066Sahrens static int 314ebedde84SEric Taylor get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc) 315fa9e4066Sahrens { 316e9dbad6fSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 317fa9e4066Sahrens 318ebedde84SEric Taylor (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name)); 319fa9e4066Sahrens 320ebedde84SEric Taylor while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) { 3217f7322feSeschrock if (errno == ENOMEM) { 322ebedde84SEric Taylor if (zcmd_expand_dst_nvlist(hdl, zc) != 0) { 32399653d4eSeschrock return (-1); 324e9dbad6fSeschrock } 3257f7322feSeschrock } else { 326fa9e4066Sahrens return (-1); 3277f7322feSeschrock } 3287f7322feSeschrock } 329ebedde84SEric Taylor return (0); 330fac3008cSeschrock } 331fac3008cSeschrock 33292241e0bSTom Erickson /* 33392241e0bSTom Erickson * Utility function to get the received properties of the given object. 33492241e0bSTom Erickson */ 33592241e0bSTom Erickson static int 33692241e0bSTom Erickson get_recvd_props_ioctl(zfs_handle_t *zhp) 33792241e0bSTom Erickson { 33892241e0bSTom Erickson libzfs_handle_t *hdl = zhp->zfs_hdl; 33992241e0bSTom Erickson nvlist_t *recvdprops; 34092241e0bSTom Erickson zfs_cmd_t zc = { 0 }; 34192241e0bSTom Erickson int err; 34292241e0bSTom Erickson 34392241e0bSTom Erickson if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) 34492241e0bSTom Erickson return (-1); 34592241e0bSTom Erickson 34692241e0bSTom Erickson (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 34792241e0bSTom Erickson 34892241e0bSTom Erickson while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) { 34992241e0bSTom Erickson if (errno == ENOMEM) { 35092241e0bSTom Erickson if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 35192241e0bSTom Erickson return (-1); 35292241e0bSTom Erickson } 35392241e0bSTom Erickson } else { 35492241e0bSTom Erickson zcmd_free_nvlists(&zc); 35592241e0bSTom Erickson return (-1); 35692241e0bSTom Erickson } 35792241e0bSTom Erickson } 35892241e0bSTom Erickson 35992241e0bSTom Erickson err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops); 36092241e0bSTom Erickson zcmd_free_nvlists(&zc); 36192241e0bSTom Erickson if (err != 0) 36292241e0bSTom Erickson return (-1); 36392241e0bSTom Erickson 36492241e0bSTom Erickson nvlist_free(zhp->zfs_recvd_props); 36592241e0bSTom Erickson zhp->zfs_recvd_props = recvdprops; 36692241e0bSTom Erickson 36792241e0bSTom Erickson return (0); 36892241e0bSTom Erickson } 36992241e0bSTom Erickson 370ebedde84SEric Taylor static int 371ebedde84SEric Taylor put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc) 372ebedde84SEric Taylor { 373ebedde84SEric Taylor nvlist_t *allprops, *userprops; 374ebedde84SEric Taylor 375ebedde84SEric Taylor zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */ 376ebedde84SEric Taylor 377ebedde84SEric Taylor if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) { 378ebedde84SEric Taylor return (-1); 379ebedde84SEric Taylor } 380fac3008cSeschrock 38114843421SMatthew Ahrens /* 38214843421SMatthew Ahrens * XXX Why do we store the user props separately, in addition to 38314843421SMatthew Ahrens * storing them in zfs_props? 38414843421SMatthew Ahrens */ 385fac3008cSeschrock if ((userprops = process_user_props(zhp, allprops)) == NULL) { 386fac3008cSeschrock nvlist_free(allprops); 387fac3008cSeschrock return (-1); 388fac3008cSeschrock } 389fac3008cSeschrock 39099653d4eSeschrock nvlist_free(zhp->zfs_props); 391fac3008cSeschrock nvlist_free(zhp->zfs_user_props); 39299653d4eSeschrock 393fac3008cSeschrock zhp->zfs_props = allprops; 394fac3008cSeschrock zhp->zfs_user_props = userprops; 39599653d4eSeschrock 396fa9e4066Sahrens return (0); 397fa9e4066Sahrens } 398fa9e4066Sahrens 399ebedde84SEric Taylor static int 400ebedde84SEric Taylor get_stats(zfs_handle_t *zhp) 401ebedde84SEric Taylor { 402ebedde84SEric Taylor int rc = 0; 403ebedde84SEric Taylor zfs_cmd_t zc = { 0 }; 404ebedde84SEric Taylor 405ebedde84SEric Taylor if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 406ebedde84SEric Taylor return (-1); 407ebedde84SEric Taylor if (get_stats_ioctl(zhp, &zc) != 0) 408ebedde84SEric Taylor rc = -1; 409ebedde84SEric Taylor else if (put_stats_zhdl(zhp, &zc) != 0) 410ebedde84SEric Taylor rc = -1; 411ebedde84SEric Taylor zcmd_free_nvlists(&zc); 412ebedde84SEric Taylor return (rc); 413ebedde84SEric Taylor } 414ebedde84SEric Taylor 415fa9e4066Sahrens /* 416fa9e4066Sahrens * Refresh the properties currently stored in the handle. 417fa9e4066Sahrens */ 418fa9e4066Sahrens void 419fa9e4066Sahrens zfs_refresh_properties(zfs_handle_t *zhp) 420fa9e4066Sahrens { 421fa9e4066Sahrens (void) get_stats(zhp); 422fa9e4066Sahrens } 423fa9e4066Sahrens 424fa9e4066Sahrens /* 425fa9e4066Sahrens * Makes a handle from the given dataset name. Used by zfs_open() and 426fa9e4066Sahrens * zfs_iter_* to create child handles on the fly. 427fa9e4066Sahrens */ 428ebedde84SEric Taylor static int 429ebedde84SEric Taylor make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc) 430fa9e4066Sahrens { 431503ad85cSMatthew Ahrens if (put_stats_zhdl(zhp, zc) != 0) 432ebedde84SEric Taylor return (-1); 43331fd60d3Sahrens 434fa9e4066Sahrens /* 435fa9e4066Sahrens * We've managed to open the dataset and gather statistics. Determine 436fa9e4066Sahrens * the high-level type. 437fa9e4066Sahrens */ 438a2eea2e1Sahrens if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) 439a2eea2e1Sahrens zhp->zfs_head_type = ZFS_TYPE_VOLUME; 440a2eea2e1Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) 441a2eea2e1Sahrens zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM; 442a2eea2e1Sahrens else 443a2eea2e1Sahrens abort(); 444a2eea2e1Sahrens 445fa9e4066Sahrens if (zhp->zfs_dmustats.dds_is_snapshot) 446fa9e4066Sahrens zhp->zfs_type = ZFS_TYPE_SNAPSHOT; 447fa9e4066Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) 448fa9e4066Sahrens zhp->zfs_type = ZFS_TYPE_VOLUME; 449fa9e4066Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) 450fa9e4066Sahrens zhp->zfs_type = ZFS_TYPE_FILESYSTEM; 451fa9e4066Sahrens else 45299653d4eSeschrock abort(); /* we should never see any other types */ 453fa9e4066Sahrens 4549fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL) 4559fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States return (-1); 4569fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 457ebedde84SEric Taylor return (0); 458ebedde84SEric Taylor } 459ebedde84SEric Taylor 460ebedde84SEric Taylor zfs_handle_t * 461ebedde84SEric Taylor make_dataset_handle(libzfs_handle_t *hdl, const char *path) 462ebedde84SEric Taylor { 463ebedde84SEric Taylor zfs_cmd_t zc = { 0 }; 464ebedde84SEric Taylor 465ebedde84SEric Taylor zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 466ebedde84SEric Taylor 467ebedde84SEric Taylor if (zhp == NULL) 468ebedde84SEric Taylor return (NULL); 469ebedde84SEric Taylor 470ebedde84SEric Taylor zhp->zfs_hdl = hdl; 471ebedde84SEric Taylor (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name)); 472ebedde84SEric Taylor if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) { 473ebedde84SEric Taylor free(zhp); 474ebedde84SEric Taylor return (NULL); 475ebedde84SEric Taylor } 476ebedde84SEric Taylor if (get_stats_ioctl(zhp, &zc) == -1) { 477ebedde84SEric Taylor zcmd_free_nvlists(&zc); 478ebedde84SEric Taylor free(zhp); 479ebedde84SEric Taylor return (NULL); 480ebedde84SEric Taylor } 481ebedde84SEric Taylor if (make_dataset_handle_common(zhp, &zc) == -1) { 482ebedde84SEric Taylor free(zhp); 483ebedde84SEric Taylor zhp = NULL; 484ebedde84SEric Taylor } 485ebedde84SEric Taylor zcmd_free_nvlists(&zc); 486ebedde84SEric Taylor return (zhp); 487ebedde84SEric Taylor } 488ebedde84SEric Taylor 48919b94df9SMatthew Ahrens zfs_handle_t * 490ebedde84SEric Taylor make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc) 491ebedde84SEric Taylor { 492ebedde84SEric Taylor zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 493ebedde84SEric Taylor 494ebedde84SEric Taylor if (zhp == NULL) 495ebedde84SEric Taylor return (NULL); 496ebedde84SEric Taylor 497ebedde84SEric Taylor zhp->zfs_hdl = hdl; 498ebedde84SEric Taylor (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name)); 499ebedde84SEric Taylor if (make_dataset_handle_common(zhp, zc) == -1) { 500ebedde84SEric Taylor free(zhp); 501ebedde84SEric Taylor return (NULL); 502ebedde84SEric Taylor } 503fa9e4066Sahrens return (zhp); 504fa9e4066Sahrens } 505fa9e4066Sahrens 50619b94df9SMatthew Ahrens zfs_handle_t * 5070d8fa8f8SMartin Matuska make_dataset_simple_handle_zc(zfs_handle_t *pzhp, zfs_cmd_t *zc) 5080d8fa8f8SMartin Matuska { 5090d8fa8f8SMartin Matuska zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 5100d8fa8f8SMartin Matuska 5110d8fa8f8SMartin Matuska if (zhp == NULL) 5120d8fa8f8SMartin Matuska return (NULL); 5130d8fa8f8SMartin Matuska 5140d8fa8f8SMartin Matuska zhp->zfs_hdl = pzhp->zfs_hdl; 5150d8fa8f8SMartin Matuska (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name)); 5160d8fa8f8SMartin Matuska zhp->zfs_head_type = pzhp->zfs_type; 5170d8fa8f8SMartin Matuska zhp->zfs_type = ZFS_TYPE_SNAPSHOT; 5180d8fa8f8SMartin Matuska zhp->zpool_hdl = zpool_handle(zhp); 5190d8fa8f8SMartin Matuska return (zhp); 5200d8fa8f8SMartin Matuska } 5210d8fa8f8SMartin Matuska 5220d8fa8f8SMartin Matuska zfs_handle_t * 52319b94df9SMatthew Ahrens zfs_handle_dup(zfs_handle_t *zhp_orig) 52419b94df9SMatthew Ahrens { 52519b94df9SMatthew Ahrens zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 52619b94df9SMatthew Ahrens 52719b94df9SMatthew Ahrens if (zhp == NULL) 52819b94df9SMatthew Ahrens return (NULL); 52919b94df9SMatthew Ahrens 53019b94df9SMatthew Ahrens zhp->zfs_hdl = zhp_orig->zfs_hdl; 53119b94df9SMatthew Ahrens zhp->zpool_hdl = zhp_orig->zpool_hdl; 53219b94df9SMatthew Ahrens (void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name, 53319b94df9SMatthew Ahrens sizeof (zhp->zfs_name)); 53419b94df9SMatthew Ahrens zhp->zfs_type = zhp_orig->zfs_type; 53519b94df9SMatthew Ahrens zhp->zfs_head_type = zhp_orig->zfs_head_type; 53619b94df9SMatthew Ahrens zhp->zfs_dmustats = zhp_orig->zfs_dmustats; 53719b94df9SMatthew Ahrens if (zhp_orig->zfs_props != NULL) { 53819b94df9SMatthew Ahrens if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) { 53919b94df9SMatthew Ahrens (void) no_memory(zhp->zfs_hdl); 54019b94df9SMatthew Ahrens zfs_close(zhp); 54119b94df9SMatthew Ahrens return (NULL); 54219b94df9SMatthew Ahrens } 54319b94df9SMatthew Ahrens } 54419b94df9SMatthew Ahrens if (zhp_orig->zfs_user_props != NULL) { 54519b94df9SMatthew Ahrens if (nvlist_dup(zhp_orig->zfs_user_props, 54619b94df9SMatthew Ahrens &zhp->zfs_user_props, 0) != 0) { 54719b94df9SMatthew Ahrens (void) no_memory(zhp->zfs_hdl); 54819b94df9SMatthew Ahrens zfs_close(zhp); 54919b94df9SMatthew Ahrens return (NULL); 55019b94df9SMatthew Ahrens } 55119b94df9SMatthew Ahrens } 55219b94df9SMatthew Ahrens if (zhp_orig->zfs_recvd_props != NULL) { 55319b94df9SMatthew Ahrens if (nvlist_dup(zhp_orig->zfs_recvd_props, 55419b94df9SMatthew Ahrens &zhp->zfs_recvd_props, 0)) { 55519b94df9SMatthew Ahrens (void) no_memory(zhp->zfs_hdl); 55619b94df9SMatthew Ahrens zfs_close(zhp); 55719b94df9SMatthew Ahrens return (NULL); 55819b94df9SMatthew Ahrens } 55919b94df9SMatthew Ahrens } 56019b94df9SMatthew Ahrens zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck; 56119b94df9SMatthew Ahrens if (zhp_orig->zfs_mntopts != NULL) { 56219b94df9SMatthew Ahrens zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl, 56319b94df9SMatthew Ahrens zhp_orig->zfs_mntopts); 56419b94df9SMatthew Ahrens } 56519b94df9SMatthew Ahrens zhp->zfs_props_table = zhp_orig->zfs_props_table; 56619b94df9SMatthew Ahrens return (zhp); 56719b94df9SMatthew Ahrens } 56819b94df9SMatthew Ahrens 56978f17100SMatthew Ahrens boolean_t 57078f17100SMatthew Ahrens zfs_bookmark_exists(const char *path) 57178f17100SMatthew Ahrens { 57278f17100SMatthew Ahrens nvlist_t *bmarks; 57378f17100SMatthew Ahrens nvlist_t *props; 5749adfa60dSMatthew Ahrens char fsname[ZFS_MAX_DATASET_NAME_LEN]; 57578f17100SMatthew Ahrens char *bmark_name; 57678f17100SMatthew Ahrens char *pound; 57778f17100SMatthew Ahrens int err; 57878f17100SMatthew Ahrens boolean_t rv; 57978f17100SMatthew Ahrens 58078f17100SMatthew Ahrens 58178f17100SMatthew Ahrens (void) strlcpy(fsname, path, sizeof (fsname)); 58278f17100SMatthew Ahrens pound = strchr(fsname, '#'); 58378f17100SMatthew Ahrens if (pound == NULL) 58478f17100SMatthew Ahrens return (B_FALSE); 58578f17100SMatthew Ahrens 58678f17100SMatthew Ahrens *pound = '\0'; 58778f17100SMatthew Ahrens bmark_name = pound + 1; 58878f17100SMatthew Ahrens props = fnvlist_alloc(); 58978f17100SMatthew Ahrens err = lzc_get_bookmarks(fsname, props, &bmarks); 59078f17100SMatthew Ahrens nvlist_free(props); 59178f17100SMatthew Ahrens if (err != 0) { 59278f17100SMatthew Ahrens nvlist_free(bmarks); 59378f17100SMatthew Ahrens return (B_FALSE); 59478f17100SMatthew Ahrens } 59578f17100SMatthew Ahrens 59678f17100SMatthew Ahrens rv = nvlist_exists(bmarks, bmark_name); 59778f17100SMatthew Ahrens nvlist_free(bmarks); 59878f17100SMatthew Ahrens return (rv); 59978f17100SMatthew Ahrens } 60078f17100SMatthew Ahrens 60178f17100SMatthew Ahrens zfs_handle_t * 60278f17100SMatthew Ahrens make_bookmark_handle(zfs_handle_t *parent, const char *path, 60378f17100SMatthew Ahrens nvlist_t *bmark_props) 60478f17100SMatthew Ahrens { 60578f17100SMatthew Ahrens zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 60678f17100SMatthew Ahrens 60778f17100SMatthew Ahrens if (zhp == NULL) 60878f17100SMatthew Ahrens return (NULL); 60978f17100SMatthew Ahrens 61078f17100SMatthew Ahrens /* Fill in the name. */ 61178f17100SMatthew Ahrens zhp->zfs_hdl = parent->zfs_hdl; 61278f17100SMatthew Ahrens (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name)); 61378f17100SMatthew Ahrens 61478f17100SMatthew Ahrens /* Set the property lists. */ 61578f17100SMatthew Ahrens if (nvlist_dup(bmark_props, &zhp->zfs_props, 0) != 0) { 61678f17100SMatthew Ahrens free(zhp); 61778f17100SMatthew Ahrens return (NULL); 61878f17100SMatthew Ahrens } 61978f17100SMatthew Ahrens 62078f17100SMatthew Ahrens /* Set the types. */ 62178f17100SMatthew Ahrens zhp->zfs_head_type = parent->zfs_head_type; 62278f17100SMatthew Ahrens zhp->zfs_type = ZFS_TYPE_BOOKMARK; 62378f17100SMatthew Ahrens 62478f17100SMatthew Ahrens if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL) { 62578f17100SMatthew Ahrens nvlist_free(zhp->zfs_props); 62678f17100SMatthew Ahrens free(zhp); 62778f17100SMatthew Ahrens return (NULL); 62878f17100SMatthew Ahrens } 62978f17100SMatthew Ahrens 63078f17100SMatthew Ahrens return (zhp); 63178f17100SMatthew Ahrens } 63278f17100SMatthew Ahrens 633edb901aaSMarcel Telka struct zfs_open_bookmarks_cb_data { 634edb901aaSMarcel Telka const char *path; 635edb901aaSMarcel Telka zfs_handle_t *zhp; 636edb901aaSMarcel Telka }; 637edb901aaSMarcel Telka 638edb901aaSMarcel Telka static int 639edb901aaSMarcel Telka zfs_open_bookmarks_cb(zfs_handle_t *zhp, void *data) 640edb901aaSMarcel Telka { 641edb901aaSMarcel Telka struct zfs_open_bookmarks_cb_data *dp = data; 642edb901aaSMarcel Telka 643fa9e4066Sahrens /* 644edb901aaSMarcel Telka * Is it the one we are looking for? 645edb901aaSMarcel Telka */ 646edb901aaSMarcel Telka if (strcmp(dp->path, zfs_get_name(zhp)) == 0) { 647edb901aaSMarcel Telka /* 648edb901aaSMarcel Telka * We found it. Save it and let the caller know we are done. 649edb901aaSMarcel Telka */ 650edb901aaSMarcel Telka dp->zhp = zhp; 651edb901aaSMarcel Telka return (EEXIST); 652edb901aaSMarcel Telka } 653edb901aaSMarcel Telka 654edb901aaSMarcel Telka /* 655edb901aaSMarcel Telka * Not found. Close the handle and ask for another one. 656edb901aaSMarcel Telka */ 657edb901aaSMarcel Telka zfs_close(zhp); 658edb901aaSMarcel Telka return (0); 659edb901aaSMarcel Telka } 660edb901aaSMarcel Telka 661edb901aaSMarcel Telka /* 662edb901aaSMarcel Telka * Opens the given snapshot, bookmark, filesystem, or volume. The 'types' 663fa9e4066Sahrens * argument is a mask of acceptable types. The function will print an 664fa9e4066Sahrens * appropriate error message and return NULL if it can't be opened. 665fa9e4066Sahrens */ 666fa9e4066Sahrens zfs_handle_t * 66799653d4eSeschrock zfs_open(libzfs_handle_t *hdl, const char *path, int types) 668fa9e4066Sahrens { 669fa9e4066Sahrens zfs_handle_t *zhp; 67099653d4eSeschrock char errbuf[1024]; 671edb901aaSMarcel Telka char *bookp; 67299653d4eSeschrock 67399653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), 67499653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), path); 675fa9e4066Sahrens 676fa9e4066Sahrens /* 67799653d4eSeschrock * Validate the name before we even try to open it. 678fa9e4066Sahrens */ 679edb901aaSMarcel Telka if (!zfs_validate_name(hdl, path, types, B_FALSE)) { 68099653d4eSeschrock (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 681fa9e4066Sahrens return (NULL); 682fa9e4066Sahrens } 683fa9e4066Sahrens 684fa9e4066Sahrens /* 685edb901aaSMarcel Telka * Bookmarks needs to be handled separately. 686edb901aaSMarcel Telka */ 687edb901aaSMarcel Telka bookp = strchr(path, '#'); 688edb901aaSMarcel Telka if (bookp == NULL) { 689edb901aaSMarcel Telka /* 690edb901aaSMarcel Telka * Try to get stats for the dataset, which will tell us if it 691edb901aaSMarcel Telka * exists. 692fa9e4066Sahrens */ 693fa9e4066Sahrens errno = 0; 69499653d4eSeschrock if ((zhp = make_dataset_handle(hdl, path)) == NULL) { 695ece3d9b3Slling (void) zfs_standard_error(hdl, errno, errbuf); 696fa9e4066Sahrens return (NULL); 697fa9e4066Sahrens } 698edb901aaSMarcel Telka } else { 699edb901aaSMarcel Telka char dsname[ZFS_MAX_DATASET_NAME_LEN]; 700edb901aaSMarcel Telka zfs_handle_t *pzhp; 701edb901aaSMarcel Telka struct zfs_open_bookmarks_cb_data cb_data = {path, NULL}; 702edb901aaSMarcel Telka 703edb901aaSMarcel Telka /* 704edb901aaSMarcel Telka * We need to cut out '#' and everything after '#' 705edb901aaSMarcel Telka * to get the parent dataset name only. 706edb901aaSMarcel Telka */ 707edb901aaSMarcel Telka assert(bookp - path < sizeof (dsname)); 708edb901aaSMarcel Telka (void) strncpy(dsname, path, bookp - path); 709edb901aaSMarcel Telka dsname[bookp - path] = '\0'; 710edb901aaSMarcel Telka 711edb901aaSMarcel Telka /* 712edb901aaSMarcel Telka * Create handle for the parent dataset. 713edb901aaSMarcel Telka */ 714edb901aaSMarcel Telka errno = 0; 715edb901aaSMarcel Telka if ((pzhp = make_dataset_handle(hdl, dsname)) == NULL) { 716edb901aaSMarcel Telka (void) zfs_standard_error(hdl, errno, errbuf); 717edb901aaSMarcel Telka return (NULL); 718edb901aaSMarcel Telka } 719edb901aaSMarcel Telka 720edb901aaSMarcel Telka /* 721edb901aaSMarcel Telka * Iterate bookmarks to find the right one. 722edb901aaSMarcel Telka */ 723edb901aaSMarcel Telka errno = 0; 724edb901aaSMarcel Telka if ((zfs_iter_bookmarks(pzhp, zfs_open_bookmarks_cb, 725edb901aaSMarcel Telka &cb_data) == 0) && (cb_data.zhp == NULL)) { 726edb901aaSMarcel Telka (void) zfs_error(hdl, EZFS_NOENT, errbuf); 727edb901aaSMarcel Telka zfs_close(pzhp); 728edb901aaSMarcel Telka return (NULL); 729edb901aaSMarcel Telka } 730edb901aaSMarcel Telka if (cb_data.zhp == NULL) { 731edb901aaSMarcel Telka (void) zfs_standard_error(hdl, errno, errbuf); 732edb901aaSMarcel Telka zfs_close(pzhp); 733edb901aaSMarcel Telka return (NULL); 734edb901aaSMarcel Telka } 735edb901aaSMarcel Telka zhp = cb_data.zhp; 736edb901aaSMarcel Telka 737edb901aaSMarcel Telka /* 738edb901aaSMarcel Telka * Cleanup. 739edb901aaSMarcel Telka */ 740edb901aaSMarcel Telka zfs_close(pzhp); 741edb901aaSMarcel Telka } 742fa9e4066Sahrens 743fa9e4066Sahrens if (!(types & zhp->zfs_type)) { 74499653d4eSeschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 74594de1d4cSeschrock zfs_close(zhp); 746fa9e4066Sahrens return (NULL); 747fa9e4066Sahrens } 748fa9e4066Sahrens 749fa9e4066Sahrens return (zhp); 750fa9e4066Sahrens } 751fa9e4066Sahrens 752fa9e4066Sahrens /* 753fa9e4066Sahrens * Release a ZFS handle. Nothing to do but free the associated memory. 754fa9e4066Sahrens */ 755fa9e4066Sahrens void 756fa9e4066Sahrens zfs_close(zfs_handle_t *zhp) 757fa9e4066Sahrens { 758fa9e4066Sahrens if (zhp->zfs_mntopts) 759fa9e4066Sahrens free(zhp->zfs_mntopts); 76099653d4eSeschrock nvlist_free(zhp->zfs_props); 761e9dbad6fSeschrock nvlist_free(zhp->zfs_user_props); 76292241e0bSTom Erickson nvlist_free(zhp->zfs_recvd_props); 763fa9e4066Sahrens free(zhp); 764fa9e4066Sahrens } 765fa9e4066Sahrens 766ebedde84SEric Taylor typedef struct mnttab_node { 767ebedde84SEric Taylor struct mnttab mtn_mt; 768ebedde84SEric Taylor avl_node_t mtn_node; 769ebedde84SEric Taylor } mnttab_node_t; 770ebedde84SEric Taylor 771ebedde84SEric Taylor static int 772ebedde84SEric Taylor libzfs_mnttab_cache_compare(const void *arg1, const void *arg2) 773ebedde84SEric Taylor { 774ebedde84SEric Taylor const mnttab_node_t *mtn1 = arg1; 775ebedde84SEric Taylor const mnttab_node_t *mtn2 = arg2; 776ebedde84SEric Taylor int rv; 777ebedde84SEric Taylor 778ebedde84SEric Taylor rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special); 779ebedde84SEric Taylor 780ebedde84SEric Taylor if (rv == 0) 781ebedde84SEric Taylor return (0); 782ebedde84SEric Taylor return (rv > 0 ? 1 : -1); 783ebedde84SEric Taylor } 784ebedde84SEric Taylor 785ebedde84SEric Taylor void 786ebedde84SEric Taylor libzfs_mnttab_init(libzfs_handle_t *hdl) 787ebedde84SEric Taylor { 788ebedde84SEric Taylor assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0); 789ebedde84SEric Taylor avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare, 790ebedde84SEric Taylor sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node)); 791b2634b9cSEric Taylor } 792b2634b9cSEric Taylor 793b2634b9cSEric Taylor void 794b2634b9cSEric Taylor libzfs_mnttab_update(libzfs_handle_t *hdl) 795b2634b9cSEric Taylor { 796b2634b9cSEric Taylor struct mnttab entry; 797ebedde84SEric Taylor 798ebedde84SEric Taylor rewind(hdl->libzfs_mnttab); 799ebedde84SEric Taylor while (getmntent(hdl->libzfs_mnttab, &entry) == 0) { 800ebedde84SEric Taylor mnttab_node_t *mtn; 801ebedde84SEric Taylor 802ebedde84SEric Taylor if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) 803ebedde84SEric Taylor continue; 804ebedde84SEric Taylor mtn = zfs_alloc(hdl, sizeof (mnttab_node_t)); 805ebedde84SEric Taylor mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special); 806ebedde84SEric Taylor mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp); 807ebedde84SEric Taylor mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype); 808ebedde84SEric Taylor mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts); 809ebedde84SEric Taylor avl_add(&hdl->libzfs_mnttab_cache, mtn); 810ebedde84SEric Taylor } 811ebedde84SEric Taylor } 812ebedde84SEric Taylor 813ebedde84SEric Taylor void 814ebedde84SEric Taylor libzfs_mnttab_fini(libzfs_handle_t *hdl) 815ebedde84SEric Taylor { 816ebedde84SEric Taylor void *cookie = NULL; 817ebedde84SEric Taylor mnttab_node_t *mtn; 818ebedde84SEric Taylor 81988f61deeSIgor Kozhukhov while ((mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie)) 82088f61deeSIgor Kozhukhov != NULL) { 821ebedde84SEric Taylor free(mtn->mtn_mt.mnt_special); 822ebedde84SEric Taylor free(mtn->mtn_mt.mnt_mountp); 823ebedde84SEric Taylor free(mtn->mtn_mt.mnt_fstype); 824ebedde84SEric Taylor free(mtn->mtn_mt.mnt_mntopts); 825ebedde84SEric Taylor free(mtn); 826ebedde84SEric Taylor } 827ebedde84SEric Taylor avl_destroy(&hdl->libzfs_mnttab_cache); 828ebedde84SEric Taylor } 829ebedde84SEric Taylor 830b2634b9cSEric Taylor void 831b2634b9cSEric Taylor libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable) 832b2634b9cSEric Taylor { 833b2634b9cSEric Taylor hdl->libzfs_mnttab_enable = enable; 834b2634b9cSEric Taylor } 835b2634b9cSEric Taylor 836ebedde84SEric Taylor int 837ebedde84SEric Taylor libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname, 838ebedde84SEric Taylor struct mnttab *entry) 839ebedde84SEric Taylor { 840ebedde84SEric Taylor mnttab_node_t find; 841ebedde84SEric Taylor mnttab_node_t *mtn; 842ebedde84SEric Taylor 843b2634b9cSEric Taylor if (!hdl->libzfs_mnttab_enable) { 844b2634b9cSEric Taylor struct mnttab srch = { 0 }; 845b2634b9cSEric Taylor 846b2634b9cSEric Taylor if (avl_numnodes(&hdl->libzfs_mnttab_cache)) 847b2634b9cSEric Taylor libzfs_mnttab_fini(hdl); 848b2634b9cSEric Taylor rewind(hdl->libzfs_mnttab); 849b2634b9cSEric Taylor srch.mnt_special = (char *)fsname; 850b2634b9cSEric Taylor srch.mnt_fstype = MNTTYPE_ZFS; 851b2634b9cSEric Taylor if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0) 852b2634b9cSEric Taylor return (0); 853b2634b9cSEric Taylor else 854b2634b9cSEric Taylor return (ENOENT); 855b2634b9cSEric Taylor } 856b2634b9cSEric Taylor 857ebedde84SEric Taylor if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0) 858b2634b9cSEric Taylor libzfs_mnttab_update(hdl); 859ebedde84SEric Taylor 860ebedde84SEric Taylor find.mtn_mt.mnt_special = (char *)fsname; 861ebedde84SEric Taylor mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL); 862ebedde84SEric Taylor if (mtn) { 863ebedde84SEric Taylor *entry = mtn->mtn_mt; 864ebedde84SEric Taylor return (0); 865ebedde84SEric Taylor } 866ebedde84SEric Taylor return (ENOENT); 867ebedde84SEric Taylor } 868ebedde84SEric Taylor 869ebedde84SEric Taylor void 870ebedde84SEric Taylor libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special, 871ebedde84SEric Taylor const char *mountp, const char *mntopts) 872ebedde84SEric Taylor { 873ebedde84SEric Taylor mnttab_node_t *mtn; 874ebedde84SEric Taylor 875ebedde84SEric Taylor if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0) 876ebedde84SEric Taylor return; 877ebedde84SEric Taylor mtn = zfs_alloc(hdl, sizeof (mnttab_node_t)); 878ebedde84SEric Taylor mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special); 879ebedde84SEric Taylor mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp); 880ebedde84SEric Taylor mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS); 881ebedde84SEric Taylor mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts); 882ebedde84SEric Taylor avl_add(&hdl->libzfs_mnttab_cache, mtn); 883ebedde84SEric Taylor } 884ebedde84SEric Taylor 885ebedde84SEric Taylor void 886ebedde84SEric Taylor libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname) 887ebedde84SEric Taylor { 888ebedde84SEric Taylor mnttab_node_t find; 889ebedde84SEric Taylor mnttab_node_t *ret; 890ebedde84SEric Taylor 891ebedde84SEric Taylor find.mtn_mt.mnt_special = (char *)fsname; 89288f61deeSIgor Kozhukhov if ((ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL)) 89388f61deeSIgor Kozhukhov != NULL) { 894ebedde84SEric Taylor avl_remove(&hdl->libzfs_mnttab_cache, ret); 895ebedde84SEric Taylor free(ret->mtn_mt.mnt_special); 896ebedde84SEric Taylor free(ret->mtn_mt.mnt_mountp); 897ebedde84SEric Taylor free(ret->mtn_mt.mnt_fstype); 898ebedde84SEric Taylor free(ret->mtn_mt.mnt_mntopts); 899ebedde84SEric Taylor free(ret); 900ebedde84SEric Taylor } 901ebedde84SEric Taylor } 902ebedde84SEric Taylor 9037b97dc1aSrm160521 int 9047b97dc1aSrm160521 zfs_spa_version(zfs_handle_t *zhp, int *spa_version) 9057b97dc1aSrm160521 { 90629ab75c9Srm160521 zpool_handle_t *zpool_handle = zhp->zpool_hdl; 9077b97dc1aSrm160521 9087b97dc1aSrm160521 if (zpool_handle == NULL) 9097b97dc1aSrm160521 return (-1); 9107b97dc1aSrm160521 9117b97dc1aSrm160521 *spa_version = zpool_get_prop_int(zpool_handle, 9127b97dc1aSrm160521 ZPOOL_PROP_VERSION, NULL); 9137b97dc1aSrm160521 return (0); 9147b97dc1aSrm160521 } 9157b97dc1aSrm160521 9167b97dc1aSrm160521 /* 9177b97dc1aSrm160521 * The choice of reservation property depends on the SPA version. 9187b97dc1aSrm160521 */ 9197b97dc1aSrm160521 static int 9207b97dc1aSrm160521 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop) 9217b97dc1aSrm160521 { 9227b97dc1aSrm160521 int spa_version; 9237b97dc1aSrm160521 9247b97dc1aSrm160521 if (zfs_spa_version(zhp, &spa_version) < 0) 9257b97dc1aSrm160521 return (-1); 9267b97dc1aSrm160521 9277b97dc1aSrm160521 if (spa_version >= SPA_VERSION_REFRESERVATION) 9287b97dc1aSrm160521 *resv_prop = ZFS_PROP_REFRESERVATION; 9297b97dc1aSrm160521 else 9307b97dc1aSrm160521 *resv_prop = ZFS_PROP_RESERVATION; 9317b97dc1aSrm160521 9327b97dc1aSrm160521 return (0); 9337b97dc1aSrm160521 } 9347b97dc1aSrm160521 935b1b8ab34Slling /* 936e9dbad6fSeschrock * Given an nvlist of properties to set, validates that they are correct, and 937e9dbad6fSeschrock * parses any numeric properties (index, boolean, etc) if they are specified as 938e9dbad6fSeschrock * strings. 939fa9e4066Sahrens */ 9400a48a24eStimh nvlist_t * 9410a48a24eStimh zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl, 942e9316f76SJoe Stein uint64_t zoned, zfs_handle_t *zhp, zpool_handle_t *zpool_hdl, 943e9316f76SJoe Stein const char *errbuf) 944fa9e4066Sahrens { 945e9dbad6fSeschrock nvpair_t *elem; 946e9dbad6fSeschrock uint64_t intval; 947e9dbad6fSeschrock char *strval; 948990b4856Slling zfs_prop_t prop; 949e9dbad6fSeschrock nvlist_t *ret; 950da6c28aaSamw int chosen_normal = -1; 951da6c28aaSamw int chosen_utf = -1; 952990b4856Slling 953e9dbad6fSeschrock if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) { 954e9dbad6fSeschrock (void) no_memory(hdl); 955e9dbad6fSeschrock return (NULL); 956e9dbad6fSeschrock } 957fa9e4066Sahrens 95814843421SMatthew Ahrens /* 95914843421SMatthew Ahrens * Make sure this property is valid and applies to this type. 96014843421SMatthew Ahrens */ 96114843421SMatthew Ahrens 962e9dbad6fSeschrock elem = NULL; 963e9dbad6fSeschrock while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { 964990b4856Slling const char *propname = nvpair_name(elem); 96599653d4eSeschrock 96614843421SMatthew Ahrens prop = zfs_name_to_prop(propname); 96714843421SMatthew Ahrens if (prop == ZPROP_INVAL && zfs_prop_user(propname)) { 968fa9e4066Sahrens /* 96914843421SMatthew Ahrens * This is a user property: make sure it's a 970990b4856Slling * string, and that it's less than ZAP_MAXNAMELEN. 971990b4856Slling */ 972990b4856Slling if (nvpair_type(elem) != DATA_TYPE_STRING) { 973990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 974990b4856Slling "'%s' must be a string"), propname); 975990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 976990b4856Slling goto error; 977990b4856Slling } 978990b4856Slling 979990b4856Slling if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) { 980e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 981e9dbad6fSeschrock "property name '%s' is too long"), 982e9dbad6fSeschrock propname); 983990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 984e9dbad6fSeschrock goto error; 985e9dbad6fSeschrock } 986e9dbad6fSeschrock 987e9dbad6fSeschrock (void) nvpair_value_string(elem, &strval); 988e9dbad6fSeschrock if (nvlist_add_string(ret, propname, strval) != 0) { 989e9dbad6fSeschrock (void) no_memory(hdl); 990e9dbad6fSeschrock goto error; 991e9dbad6fSeschrock } 992e9dbad6fSeschrock continue; 993e9dbad6fSeschrock } 994fa9e4066Sahrens 99514843421SMatthew Ahrens /* 99614843421SMatthew Ahrens * Currently, only user properties can be modified on 99714843421SMatthew Ahrens * snapshots. 99814843421SMatthew Ahrens */ 999bb0ade09Sahrens if (type == ZFS_TYPE_SNAPSHOT) { 1000bb0ade09Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1001bb0ade09Sahrens "this property can not be modified for snapshots")); 1002bb0ade09Sahrens (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 1003bb0ade09Sahrens goto error; 1004bb0ade09Sahrens } 1005bb0ade09Sahrens 100614843421SMatthew Ahrens if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) { 100714843421SMatthew Ahrens zfs_userquota_prop_t uqtype; 100814843421SMatthew Ahrens char newpropname[128]; 100914843421SMatthew Ahrens char domain[128]; 101014843421SMatthew Ahrens uint64_t rid; 101114843421SMatthew Ahrens uint64_t valary[3]; 101214843421SMatthew Ahrens 101314843421SMatthew Ahrens if (userquota_propname_decode(propname, zoned, 101414843421SMatthew Ahrens &uqtype, domain, sizeof (domain), &rid) != 0) { 101514843421SMatthew Ahrens zfs_error_aux(hdl, 101614843421SMatthew Ahrens dgettext(TEXT_DOMAIN, 101714843421SMatthew Ahrens "'%s' has an invalid user/group name"), 101814843421SMatthew Ahrens propname); 101914843421SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 102014843421SMatthew Ahrens goto error; 102114843421SMatthew Ahrens } 102214843421SMatthew Ahrens 102314843421SMatthew Ahrens if (uqtype != ZFS_PROP_USERQUOTA && 102414843421SMatthew Ahrens uqtype != ZFS_PROP_GROUPQUOTA) { 102514843421SMatthew Ahrens zfs_error_aux(hdl, 102614843421SMatthew Ahrens dgettext(TEXT_DOMAIN, "'%s' is readonly"), 102714843421SMatthew Ahrens propname); 102814843421SMatthew Ahrens (void) zfs_error(hdl, EZFS_PROPREADONLY, 102914843421SMatthew Ahrens errbuf); 103014843421SMatthew Ahrens goto error; 103114843421SMatthew Ahrens } 103214843421SMatthew Ahrens 103314843421SMatthew Ahrens if (nvpair_type(elem) == DATA_TYPE_STRING) { 103414843421SMatthew Ahrens (void) nvpair_value_string(elem, &strval); 103514843421SMatthew Ahrens if (strcmp(strval, "none") == 0) { 103614843421SMatthew Ahrens intval = 0; 103714843421SMatthew Ahrens } else if (zfs_nicestrtonum(hdl, 103814843421SMatthew Ahrens strval, &intval) != 0) { 103914843421SMatthew Ahrens (void) zfs_error(hdl, 104014843421SMatthew Ahrens EZFS_BADPROP, errbuf); 104114843421SMatthew Ahrens goto error; 104214843421SMatthew Ahrens } 104314843421SMatthew Ahrens } else if (nvpair_type(elem) == 104414843421SMatthew Ahrens DATA_TYPE_UINT64) { 104514843421SMatthew Ahrens (void) nvpair_value_uint64(elem, &intval); 104614843421SMatthew Ahrens if (intval == 0) { 104714843421SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 104814843421SMatthew Ahrens "use 'none' to disable " 104914843421SMatthew Ahrens "userquota/groupquota")); 105014843421SMatthew Ahrens goto error; 105114843421SMatthew Ahrens } 105214843421SMatthew Ahrens } else { 105314843421SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 105414843421SMatthew Ahrens "'%s' must be a number"), propname); 105514843421SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 105614843421SMatthew Ahrens goto error; 105714843421SMatthew Ahrens } 105814843421SMatthew Ahrens 10592d5843dbSMatthew Ahrens /* 10602d5843dbSMatthew Ahrens * Encode the prop name as 10612d5843dbSMatthew Ahrens * userquota@<hex-rid>-domain, to make it easy 10622d5843dbSMatthew Ahrens * for the kernel to decode. 10632d5843dbSMatthew Ahrens */ 106414843421SMatthew Ahrens (void) snprintf(newpropname, sizeof (newpropname), 10652d5843dbSMatthew Ahrens "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype], 10662d5843dbSMatthew Ahrens (longlong_t)rid, domain); 106714843421SMatthew Ahrens valary[0] = uqtype; 106814843421SMatthew Ahrens valary[1] = rid; 106914843421SMatthew Ahrens valary[2] = intval; 107014843421SMatthew Ahrens if (nvlist_add_uint64_array(ret, newpropname, 107114843421SMatthew Ahrens valary, 3) != 0) { 107214843421SMatthew Ahrens (void) no_memory(hdl); 107314843421SMatthew Ahrens goto error; 107414843421SMatthew Ahrens } 107514843421SMatthew Ahrens continue; 107619b94df9SMatthew Ahrens } else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) { 107719b94df9SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 107819b94df9SMatthew Ahrens "'%s' is readonly"), 107919b94df9SMatthew Ahrens propname); 108019b94df9SMatthew Ahrens (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 108119b94df9SMatthew Ahrens goto error; 108214843421SMatthew Ahrens } 108314843421SMatthew Ahrens 108414843421SMatthew Ahrens if (prop == ZPROP_INVAL) { 108514843421SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 108614843421SMatthew Ahrens "invalid property '%s'"), propname); 108714843421SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 108814843421SMatthew Ahrens goto error; 108914843421SMatthew Ahrens } 109014843421SMatthew Ahrens 1091e9dbad6fSeschrock if (!zfs_prop_valid_for_type(prop, type)) { 1092e9dbad6fSeschrock zfs_error_aux(hdl, 1093e9dbad6fSeschrock dgettext(TEXT_DOMAIN, "'%s' does not " 1094e9dbad6fSeschrock "apply to datasets of this type"), propname); 1095e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 1096e9dbad6fSeschrock goto error; 1097e9dbad6fSeschrock } 1098e9dbad6fSeschrock 1099e9dbad6fSeschrock if (zfs_prop_readonly(prop) && 1100da6c28aaSamw (!zfs_prop_setonce(prop) || zhp != NULL)) { 1101e9dbad6fSeschrock zfs_error_aux(hdl, 1102e9dbad6fSeschrock dgettext(TEXT_DOMAIN, "'%s' is readonly"), 1103e9dbad6fSeschrock propname); 1104e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 1105e9dbad6fSeschrock goto error; 1106e9dbad6fSeschrock } 1107e9dbad6fSeschrock 1108990b4856Slling if (zprop_parse_value(hdl, elem, prop, type, ret, 1109990b4856Slling &strval, &intval, errbuf) != 0) 1110e9dbad6fSeschrock goto error; 1111e9dbad6fSeschrock 1112e9dbad6fSeschrock /* 1113e9dbad6fSeschrock * Perform some additional checks for specific properties. 1114e9dbad6fSeschrock */ 1115e9dbad6fSeschrock switch (prop) { 1116e7437265Sahrens case ZFS_PROP_VERSION: 1117e7437265Sahrens { 1118e7437265Sahrens int version; 1119e7437265Sahrens 1120e7437265Sahrens if (zhp == NULL) 1121e7437265Sahrens break; 1122e7437265Sahrens version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION); 1123e7437265Sahrens if (intval < version) { 1124e7437265Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1125e7437265Sahrens "Can not downgrade; already at version %u"), 1126e7437265Sahrens version); 1127e7437265Sahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1128e7437265Sahrens goto error; 1129e7437265Sahrens } 1130e7437265Sahrens break; 1131e7437265Sahrens } 1132e7437265Sahrens 1133e9dbad6fSeschrock case ZFS_PROP_VOLBLOCKSIZE: 1134b5152584SMatthew Ahrens case ZFS_PROP_RECORDSIZE: 1135b5152584SMatthew Ahrens { 1136b5152584SMatthew Ahrens int maxbs = SPA_MAXBLOCKSIZE; 1137e9316f76SJoe Stein if (zpool_hdl != NULL) { 1138e9316f76SJoe Stein maxbs = zpool_get_prop_int(zpool_hdl, 1139b5152584SMatthew Ahrens ZPOOL_PROP_MAXBLOCKSIZE, NULL); 1140b5152584SMatthew Ahrens } 1141b5152584SMatthew Ahrens /* 1142b5152584SMatthew Ahrens * Volumes are limited to a volblocksize of 128KB, 1143b5152584SMatthew Ahrens * because they typically service workloads with 1144b5152584SMatthew Ahrens * small random writes, which incur a large performance 1145b5152584SMatthew Ahrens * penalty with large blocks. 1146b5152584SMatthew Ahrens */ 1147b5152584SMatthew Ahrens if (prop == ZFS_PROP_VOLBLOCKSIZE) 1148b5152584SMatthew Ahrens maxbs = SPA_OLD_MAXBLOCKSIZE; 1149b5152584SMatthew Ahrens /* 1150b5152584SMatthew Ahrens * The value must be a power of two between 1151b5152584SMatthew Ahrens * SPA_MINBLOCKSIZE and maxbs. 1152b5152584SMatthew Ahrens */ 1153e9dbad6fSeschrock if (intval < SPA_MINBLOCKSIZE || 1154b5152584SMatthew Ahrens intval > maxbs || !ISP2(intval)) { 1155e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1156b5152584SMatthew Ahrens "'%s' must be power of 2 from 512B " 1157b5152584SMatthew Ahrens "to %uKB"), propname, maxbs >> 10); 1158e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1159e9dbad6fSeschrock goto error; 1160e9dbad6fSeschrock } 1161e9dbad6fSeschrock break; 1162b5152584SMatthew Ahrens } 11634201a95eSRic Aleshire case ZFS_PROP_MLSLABEL: 11644201a95eSRic Aleshire { 11654201a95eSRic Aleshire /* 11664201a95eSRic Aleshire * Verify the mlslabel string and convert to 11674201a95eSRic Aleshire * internal hex label string. 11684201a95eSRic Aleshire */ 11694201a95eSRic Aleshire 11704201a95eSRic Aleshire m_label_t *new_sl; 11714201a95eSRic Aleshire char *hex = NULL; /* internal label string */ 11724201a95eSRic Aleshire 11734201a95eSRic Aleshire /* Default value is already OK. */ 11744201a95eSRic Aleshire if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0) 11754201a95eSRic Aleshire break; 11764201a95eSRic Aleshire 11774201a95eSRic Aleshire /* Verify the label can be converted to binary form */ 11784201a95eSRic Aleshire if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) || 11794201a95eSRic Aleshire (str_to_label(strval, &new_sl, MAC_LABEL, 11804201a95eSRic Aleshire L_NO_CORRECTION, NULL) == -1)) { 11814201a95eSRic Aleshire goto badlabel; 11824201a95eSRic Aleshire } 11834201a95eSRic Aleshire 11844201a95eSRic Aleshire /* Now translate to hex internal label string */ 11854201a95eSRic Aleshire if (label_to_str(new_sl, &hex, M_INTERNAL, 11864201a95eSRic Aleshire DEF_NAMES) != 0) { 11874201a95eSRic Aleshire if (hex) 11884201a95eSRic Aleshire free(hex); 11894201a95eSRic Aleshire goto badlabel; 11904201a95eSRic Aleshire } 11914201a95eSRic Aleshire m_label_free(new_sl); 11924201a95eSRic Aleshire 11934201a95eSRic Aleshire /* If string is already in internal form, we're done. */ 11944201a95eSRic Aleshire if (strcmp(strval, hex) == 0) { 11954201a95eSRic Aleshire free(hex); 11964201a95eSRic Aleshire break; 11974201a95eSRic Aleshire } 11984201a95eSRic Aleshire 11994201a95eSRic Aleshire /* Replace the label string with the internal form. */ 1200569038c9SRic Aleshire (void) nvlist_remove(ret, zfs_prop_to_name(prop), 12014201a95eSRic Aleshire DATA_TYPE_STRING); 12024201a95eSRic Aleshire verify(nvlist_add_string(ret, zfs_prop_to_name(prop), 12034201a95eSRic Aleshire hex) == 0); 12044201a95eSRic Aleshire free(hex); 12054201a95eSRic Aleshire 12064201a95eSRic Aleshire break; 12074201a95eSRic Aleshire 12084201a95eSRic Aleshire badlabel: 12094201a95eSRic Aleshire zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 12104201a95eSRic Aleshire "invalid mlslabel '%s'"), strval); 12114201a95eSRic Aleshire (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 12124201a95eSRic Aleshire m_label_free(new_sl); /* OK if null */ 12134201a95eSRic Aleshire goto error; 12144201a95eSRic Aleshire 12154201a95eSRic Aleshire } 12164201a95eSRic Aleshire 1217e9dbad6fSeschrock case ZFS_PROP_MOUNTPOINT: 121889eef05eSrm160521 { 121989eef05eSrm160521 namecheck_err_t why; 122089eef05eSrm160521 1221e9dbad6fSeschrock if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 || 1222e9dbad6fSeschrock strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0) 1223e9dbad6fSeschrock break; 1224e9dbad6fSeschrock 122589eef05eSrm160521 if (mountpoint_namecheck(strval, &why)) { 122689eef05eSrm160521 switch (why) { 122789eef05eSrm160521 case NAME_ERR_LEADING_SLASH: 122889eef05eSrm160521 zfs_error_aux(hdl, 122989eef05eSrm160521 dgettext(TEXT_DOMAIN, 1230e9dbad6fSeschrock "'%s' must be an absolute path, " 1231e9dbad6fSeschrock "'none', or 'legacy'"), propname); 123289eef05eSrm160521 break; 123389eef05eSrm160521 case NAME_ERR_TOOLONG: 123489eef05eSrm160521 zfs_error_aux(hdl, 123589eef05eSrm160521 dgettext(TEXT_DOMAIN, 123689eef05eSrm160521 "component of '%s' is too long"), 123789eef05eSrm160521 propname); 123889eef05eSrm160521 break; 123988f61deeSIgor Kozhukhov 124088f61deeSIgor Kozhukhov default: 124188f61deeSIgor Kozhukhov zfs_error_aux(hdl, 124288f61deeSIgor Kozhukhov dgettext(TEXT_DOMAIN, 124388f61deeSIgor Kozhukhov "(%d) not defined"), 124488f61deeSIgor Kozhukhov why); 124588f61deeSIgor Kozhukhov break; 124689eef05eSrm160521 } 1247e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1248e9dbad6fSeschrock goto error; 1249e9dbad6fSeschrock } 125089eef05eSrm160521 } 125189eef05eSrm160521 1252f3861e1aSahl /*FALLTHRU*/ 1253e9dbad6fSeschrock 1254da6c28aaSamw case ZFS_PROP_SHARESMB: 1255f3861e1aSahl case ZFS_PROP_SHARENFS: 1256e9dbad6fSeschrock /* 1257da6c28aaSamw * For the mountpoint and sharenfs or sharesmb 1258da6c28aaSamw * properties, check if it can be set in a 1259da6c28aaSamw * global/non-global zone based on 1260f3861e1aSahl * the zoned property value: 1261fa9e4066Sahrens * 1262fa9e4066Sahrens * global zone non-global zone 1263f3861e1aSahl * -------------------------------------------------- 1264fa9e4066Sahrens * zoned=on mountpoint (no) mountpoint (yes) 1265fa9e4066Sahrens * sharenfs (no) sharenfs (no) 1266da6c28aaSamw * sharesmb (no) sharesmb (no) 1267fa9e4066Sahrens * 1268fa9e4066Sahrens * zoned=off mountpoint (yes) N/A 1269fa9e4066Sahrens * sharenfs (yes) 1270da6c28aaSamw * sharesmb (yes) 1271fa9e4066Sahrens */ 1272e9dbad6fSeschrock if (zoned) { 1273fa9e4066Sahrens if (getzoneid() == GLOBAL_ZONEID) { 127499653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1275e9dbad6fSeschrock "'%s' cannot be set on " 1276e9dbad6fSeschrock "dataset in a non-global zone"), 1277e9dbad6fSeschrock propname); 1278e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_ZONED, 1279e9dbad6fSeschrock errbuf); 1280e9dbad6fSeschrock goto error; 1281da6c28aaSamw } else if (prop == ZFS_PROP_SHARENFS || 1282da6c28aaSamw prop == ZFS_PROP_SHARESMB) { 128399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1284e9dbad6fSeschrock "'%s' cannot be set in " 1285e9dbad6fSeschrock "a non-global zone"), propname); 1286e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_ZONED, 1287e9dbad6fSeschrock errbuf); 1288e9dbad6fSeschrock goto error; 1289fa9e4066Sahrens } 1290fa9e4066Sahrens } else if (getzoneid() != GLOBAL_ZONEID) { 1291fa9e4066Sahrens /* 1292fa9e4066Sahrens * If zoned property is 'off', this must be in 129314843421SMatthew Ahrens * a global zone. If not, something is wrong. 1294fa9e4066Sahrens */ 129599653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1296e9dbad6fSeschrock "'%s' cannot be set while dataset " 1297e9dbad6fSeschrock "'zoned' property is set"), propname); 1298e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_ZONED, errbuf); 1299e9dbad6fSeschrock goto error; 1300fa9e4066Sahrens } 1301f3861e1aSahl 130267331909Sdougm /* 130367331909Sdougm * At this point, it is legitimate to set the 130467331909Sdougm * property. Now we want to make sure that the 130567331909Sdougm * property value is valid if it is sharenfs. 130667331909Sdougm */ 1307da6c28aaSamw if ((prop == ZFS_PROP_SHARENFS || 1308da6c28aaSamw prop == ZFS_PROP_SHARESMB) && 130967331909Sdougm strcmp(strval, "on") != 0 && 131067331909Sdougm strcmp(strval, "off") != 0) { 1311da6c28aaSamw zfs_share_proto_t proto; 1312da6c28aaSamw 1313da6c28aaSamw if (prop == ZFS_PROP_SHARESMB) 1314da6c28aaSamw proto = PROTO_SMB; 1315da6c28aaSamw else 1316da6c28aaSamw proto = PROTO_NFS; 131767331909Sdougm 131867331909Sdougm /* 1319da6c28aaSamw * Must be an valid sharing protocol 1320da6c28aaSamw * option string so init the libshare 1321da6c28aaSamw * in order to enable the parser and 1322da6c28aaSamw * then parse the options. We use the 1323da6c28aaSamw * control API since we don't care about 1324da6c28aaSamw * the current configuration and don't 132567331909Sdougm * want the overhead of loading it 132667331909Sdougm * until we actually do something. 132767331909Sdougm */ 132867331909Sdougm 132967331909Sdougm if (zfs_init_libshare(hdl, 133067331909Sdougm SA_INIT_CONTROL_API) != SA_OK) { 1331fac3008cSeschrock /* 1332fac3008cSeschrock * An error occurred so we can't do 1333fac3008cSeschrock * anything 1334fac3008cSeschrock */ 133567331909Sdougm zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 133667331909Sdougm "'%s' cannot be set: problem " 133767331909Sdougm "in share initialization"), 133867331909Sdougm propname); 1339fac3008cSeschrock (void) zfs_error(hdl, EZFS_BADPROP, 1340fac3008cSeschrock errbuf); 134167331909Sdougm goto error; 134267331909Sdougm } 134367331909Sdougm 1344da6c28aaSamw if (zfs_parse_options(strval, proto) != SA_OK) { 134567331909Sdougm /* 134667331909Sdougm * There was an error in parsing so 134767331909Sdougm * deal with it by issuing an error 134867331909Sdougm * message and leaving after 134967331909Sdougm * uninitializing the the libshare 135067331909Sdougm * interface. 135167331909Sdougm */ 135267331909Sdougm zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 135367331909Sdougm "'%s' cannot be set to invalid " 135467331909Sdougm "options"), propname); 1355fac3008cSeschrock (void) zfs_error(hdl, EZFS_BADPROP, 1356fac3008cSeschrock errbuf); 135767331909Sdougm zfs_uninit_libshare(hdl); 135867331909Sdougm goto error; 135967331909Sdougm } 136067331909Sdougm zfs_uninit_libshare(hdl); 136167331909Sdougm } 136267331909Sdougm 1363f3861e1aSahl break; 136488f61deeSIgor Kozhukhov 1365da6c28aaSamw case ZFS_PROP_UTF8ONLY: 1366da6c28aaSamw chosen_utf = (int)intval; 1367da6c28aaSamw break; 136888f61deeSIgor Kozhukhov 1369da6c28aaSamw case ZFS_PROP_NORMALIZE: 1370da6c28aaSamw chosen_normal = (int)intval; 1371da6c28aaSamw break; 137288f61deeSIgor Kozhukhov 137388f61deeSIgor Kozhukhov default: 137488f61deeSIgor Kozhukhov break; 1375fa9e4066Sahrens } 1376fa9e4066Sahrens 1377e9dbad6fSeschrock /* 1378e9dbad6fSeschrock * For changes to existing volumes, we have some additional 1379e9dbad6fSeschrock * checks to enforce. 1380e9dbad6fSeschrock */ 1381e9dbad6fSeschrock if (type == ZFS_TYPE_VOLUME && zhp != NULL) { 1382e9dbad6fSeschrock uint64_t volsize = zfs_prop_get_int(zhp, 1383e9dbad6fSeschrock ZFS_PROP_VOLSIZE); 1384e9dbad6fSeschrock uint64_t blocksize = zfs_prop_get_int(zhp, 1385e9dbad6fSeschrock ZFS_PROP_VOLBLOCKSIZE); 1386e9dbad6fSeschrock char buf[64]; 1387e9dbad6fSeschrock 1388e9dbad6fSeschrock switch (prop) { 1389e9dbad6fSeschrock case ZFS_PROP_RESERVATION: 1390a9799022Sck153898 case ZFS_PROP_REFRESERVATION: 1391e9dbad6fSeschrock if (intval > volsize) { 1392e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1393e9dbad6fSeschrock "'%s' is greater than current " 1394e9dbad6fSeschrock "volume size"), propname); 1395e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_BADPROP, 1396e9dbad6fSeschrock errbuf); 1397e9dbad6fSeschrock goto error; 1398e9dbad6fSeschrock } 1399e9dbad6fSeschrock break; 1400e9dbad6fSeschrock 1401e9dbad6fSeschrock case ZFS_PROP_VOLSIZE: 1402e9dbad6fSeschrock if (intval % blocksize != 0) { 1403e9dbad6fSeschrock zfs_nicenum(blocksize, buf, 1404e9dbad6fSeschrock sizeof (buf)); 1405e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1406e9dbad6fSeschrock "'%s' must be a multiple of " 1407e9dbad6fSeschrock "volume block size (%s)"), 1408e9dbad6fSeschrock propname, buf); 1409e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_BADPROP, 1410e9dbad6fSeschrock errbuf); 1411e9dbad6fSeschrock goto error; 1412e9dbad6fSeschrock } 1413e9dbad6fSeschrock 1414e9dbad6fSeschrock if (intval == 0) { 1415e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1416e9dbad6fSeschrock "'%s' cannot be zero"), 1417e9dbad6fSeschrock propname); 1418e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_BADPROP, 1419e9dbad6fSeschrock errbuf); 1420e9dbad6fSeschrock goto error; 1421e9dbad6fSeschrock } 1422f3861e1aSahl break; 142388f61deeSIgor Kozhukhov 142488f61deeSIgor Kozhukhov default: 142588f61deeSIgor Kozhukhov break; 1426e9dbad6fSeschrock } 1427e9dbad6fSeschrock } 1428e9dbad6fSeschrock } 1429e9dbad6fSeschrock 1430e9dbad6fSeschrock /* 1431da6c28aaSamw * If normalization was chosen, but no UTF8 choice was made, 1432da6c28aaSamw * enforce rejection of non-UTF8 names. 1433da6c28aaSamw * 1434da6c28aaSamw * If normalization was chosen, but rejecting non-UTF8 names 1435da6c28aaSamw * was explicitly not chosen, it is an error. 1436da6c28aaSamw */ 1437de8267e0Stimh if (chosen_normal > 0 && chosen_utf < 0) { 1438da6c28aaSamw if (nvlist_add_uint64(ret, 1439da6c28aaSamw zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) { 1440da6c28aaSamw (void) no_memory(hdl); 1441da6c28aaSamw goto error; 1442da6c28aaSamw } 1443de8267e0Stimh } else if (chosen_normal > 0 && chosen_utf == 0) { 1444da6c28aaSamw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1445da6c28aaSamw "'%s' must be set 'on' if normalization chosen"), 1446da6c28aaSamw zfs_prop_to_name(ZFS_PROP_UTF8ONLY)); 1447da6c28aaSamw (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1448da6c28aaSamw goto error; 1449da6c28aaSamw } 1450e9dbad6fSeschrock return (ret); 1451e9dbad6fSeschrock 1452e9dbad6fSeschrock error: 1453e9dbad6fSeschrock nvlist_free(ret); 1454e9dbad6fSeschrock return (NULL); 1455e9dbad6fSeschrock } 1456e9dbad6fSeschrock 145736db6475SEric Taylor int 145836db6475SEric Taylor zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl) 145936db6475SEric Taylor { 146036db6475SEric Taylor uint64_t old_volsize; 146136db6475SEric Taylor uint64_t new_volsize; 146236db6475SEric Taylor uint64_t old_reservation; 146336db6475SEric Taylor uint64_t new_reservation; 146436db6475SEric Taylor zfs_prop_t resv_prop; 1465c61ea566SGeorge Wilson nvlist_t *props; 146636db6475SEric Taylor 146736db6475SEric Taylor /* 146836db6475SEric Taylor * If this is an existing volume, and someone is setting the volsize, 146936db6475SEric Taylor * make sure that it matches the reservation, or add it if necessary. 147036db6475SEric Taylor */ 147136db6475SEric Taylor old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 147236db6475SEric Taylor if (zfs_which_resv_prop(zhp, &resv_prop) < 0) 147336db6475SEric Taylor return (-1); 147436db6475SEric Taylor old_reservation = zfs_prop_get_int(zhp, resv_prop); 1475c61ea566SGeorge Wilson 1476c61ea566SGeorge Wilson props = fnvlist_alloc(); 1477c61ea566SGeorge Wilson fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 1478c61ea566SGeorge Wilson zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE)); 1479c61ea566SGeorge Wilson 1480c61ea566SGeorge Wilson if ((zvol_volsize_to_reservation(old_volsize, props) != 1481c61ea566SGeorge Wilson old_reservation) || nvlist_exists(nvl, 1482c61ea566SGeorge Wilson zfs_prop_to_name(resv_prop))) { 1483c61ea566SGeorge Wilson fnvlist_free(props); 148436db6475SEric Taylor return (0); 148536db6475SEric Taylor } 148636db6475SEric Taylor if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE), 1487c61ea566SGeorge Wilson &new_volsize) != 0) { 1488c61ea566SGeorge Wilson fnvlist_free(props); 148936db6475SEric Taylor return (-1); 1490c61ea566SGeorge Wilson } 1491c61ea566SGeorge Wilson new_reservation = zvol_volsize_to_reservation(new_volsize, props); 1492c61ea566SGeorge Wilson fnvlist_free(props); 1493c61ea566SGeorge Wilson 149436db6475SEric Taylor if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop), 149536db6475SEric Taylor new_reservation) != 0) { 149636db6475SEric Taylor (void) no_memory(zhp->zfs_hdl); 149736db6475SEric Taylor return (-1); 149836db6475SEric Taylor } 149936db6475SEric Taylor return (1); 150036db6475SEric Taylor } 150136db6475SEric Taylor 150292241e0bSTom Erickson void 150392241e0bSTom Erickson zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err, 150492241e0bSTom Erickson char *errbuf) 150592241e0bSTom Erickson { 150692241e0bSTom Erickson switch (err) { 150792241e0bSTom Erickson 150892241e0bSTom Erickson case ENOSPC: 150992241e0bSTom Erickson /* 151092241e0bSTom Erickson * For quotas and reservations, ENOSPC indicates 151192241e0bSTom Erickson * something different; setting a quota or reservation 151292241e0bSTom Erickson * doesn't use any disk space. 151392241e0bSTom Erickson */ 151492241e0bSTom Erickson switch (prop) { 151592241e0bSTom Erickson case ZFS_PROP_QUOTA: 151692241e0bSTom Erickson case ZFS_PROP_REFQUOTA: 151792241e0bSTom Erickson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 151892241e0bSTom Erickson "size is less than current used or " 151992241e0bSTom Erickson "reserved space")); 152092241e0bSTom Erickson (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 152192241e0bSTom Erickson break; 152292241e0bSTom Erickson 152392241e0bSTom Erickson case ZFS_PROP_RESERVATION: 152492241e0bSTom Erickson case ZFS_PROP_REFRESERVATION: 152592241e0bSTom Erickson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 152692241e0bSTom Erickson "size is greater than available space")); 152792241e0bSTom Erickson (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 152892241e0bSTom Erickson break; 152992241e0bSTom Erickson 153092241e0bSTom Erickson default: 153192241e0bSTom Erickson (void) zfs_standard_error(hdl, err, errbuf); 153292241e0bSTom Erickson break; 153392241e0bSTom Erickson } 153492241e0bSTom Erickson break; 153592241e0bSTom Erickson 153692241e0bSTom Erickson case EBUSY: 153792241e0bSTom Erickson (void) zfs_standard_error(hdl, EBUSY, errbuf); 153892241e0bSTom Erickson break; 153992241e0bSTom Erickson 154092241e0bSTom Erickson case EROFS: 154192241e0bSTom Erickson (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf); 154292241e0bSTom Erickson break; 154392241e0bSTom Erickson 15446fdcb3d1SWill Andrews case E2BIG: 15456fdcb3d1SWill Andrews zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 15466fdcb3d1SWill Andrews "property value too long")); 15476fdcb3d1SWill Andrews (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 15486fdcb3d1SWill Andrews break; 15496fdcb3d1SWill Andrews 155092241e0bSTom Erickson case ENOTSUP: 155192241e0bSTom Erickson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 155292241e0bSTom Erickson "pool and or dataset must be upgraded to set this " 155392241e0bSTom Erickson "property or value")); 155492241e0bSTom Erickson (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 155592241e0bSTom Erickson break; 155692241e0bSTom Erickson 155792241e0bSTom Erickson case ERANGE: 1558b5152584SMatthew Ahrens if (prop == ZFS_PROP_COMPRESSION || 1559b5152584SMatthew Ahrens prop == ZFS_PROP_RECORDSIZE) { 156092241e0bSTom Erickson (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 156192241e0bSTom Erickson "property setting is not allowed on " 156292241e0bSTom Erickson "bootable datasets")); 156392241e0bSTom Erickson (void) zfs_error(hdl, EZFS_NOTSUP, errbuf); 156445818ee1SMatthew Ahrens } else if (prop == ZFS_PROP_CHECKSUM || 156545818ee1SMatthew Ahrens prop == ZFS_PROP_DEDUP) { 156645818ee1SMatthew Ahrens (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 156745818ee1SMatthew Ahrens "property setting is not allowed on " 156845818ee1SMatthew Ahrens "root pools")); 156945818ee1SMatthew Ahrens (void) zfs_error(hdl, EZFS_NOTSUP, errbuf); 157092241e0bSTom Erickson } else { 157192241e0bSTom Erickson (void) zfs_standard_error(hdl, err, errbuf); 157292241e0bSTom Erickson } 157392241e0bSTom Erickson break; 157492241e0bSTom Erickson 1575ab003da8SJim Dunham case EINVAL: 1576ab003da8SJim Dunham if (prop == ZPROP_INVAL) { 1577ab003da8SJim Dunham (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1578ab003da8SJim Dunham } else { 1579ab003da8SJim Dunham (void) zfs_standard_error(hdl, err, errbuf); 1580ab003da8SJim Dunham } 1581ab003da8SJim Dunham break; 1582ab003da8SJim Dunham 158392241e0bSTom Erickson case EOVERFLOW: 158492241e0bSTom Erickson /* 158592241e0bSTom Erickson * This platform can't address a volume this big. 158692241e0bSTom Erickson */ 158792241e0bSTom Erickson #ifdef _ILP32 158892241e0bSTom Erickson if (prop == ZFS_PROP_VOLSIZE) { 158992241e0bSTom Erickson (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf); 159092241e0bSTom Erickson break; 159192241e0bSTom Erickson } 159292241e0bSTom Erickson #endif 159392241e0bSTom Erickson /* FALLTHROUGH */ 159492241e0bSTom Erickson default: 159592241e0bSTom Erickson (void) zfs_standard_error(hdl, err, errbuf); 159692241e0bSTom Erickson } 159792241e0bSTom Erickson } 159892241e0bSTom Erickson 1599e9dbad6fSeschrock /* 1600e9dbad6fSeschrock * Given a property name and value, set the property for the given dataset. 1601e9dbad6fSeschrock */ 1602e9dbad6fSeschrock int 1603e9dbad6fSeschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval) 1604e9dbad6fSeschrock { 1605e9dbad6fSeschrock int ret = -1; 1606e9dbad6fSeschrock char errbuf[1024]; 1607e9dbad6fSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 160830925561SChris Williamson nvlist_t *nvl = NULL; 1609e9dbad6fSeschrock 1610e9dbad6fSeschrock (void) snprintf(errbuf, sizeof (errbuf), 1611e9dbad6fSeschrock dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 1612e9dbad6fSeschrock zhp->zfs_name); 1613e9dbad6fSeschrock 1614e9dbad6fSeschrock if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 || 1615e9dbad6fSeschrock nvlist_add_string(nvl, propname, propval) != 0) { 1616e9dbad6fSeschrock (void) no_memory(hdl); 1617e9dbad6fSeschrock goto error; 1618e9dbad6fSeschrock } 1619e9dbad6fSeschrock 162030925561SChris Williamson ret = zfs_prop_set_list(zhp, nvl); 162130925561SChris Williamson 162230925561SChris Williamson error: 162330925561SChris Williamson nvlist_free(nvl); 162430925561SChris Williamson return (ret); 162530925561SChris Williamson } 162630925561SChris Williamson 162730925561SChris Williamson 162830925561SChris Williamson 162930925561SChris Williamson /* 163030925561SChris Williamson * Given an nvlist of property names and values, set the properties for the 163130925561SChris Williamson * given dataset. 163230925561SChris Williamson */ 163330925561SChris Williamson int 163430925561SChris Williamson zfs_prop_set_list(zfs_handle_t *zhp, nvlist_t *props) 163530925561SChris Williamson { 163630925561SChris Williamson zfs_cmd_t zc = { 0 }; 163730925561SChris Williamson int ret = -1; 163830925561SChris Williamson prop_changelist_t **cls = NULL; 163930925561SChris Williamson int cl_idx; 164030925561SChris Williamson char errbuf[1024]; 164130925561SChris Williamson libzfs_handle_t *hdl = zhp->zfs_hdl; 164230925561SChris Williamson nvlist_t *nvl; 164330925561SChris Williamson int nvl_len; 1644f83b46baSPaul Dagnelie int added_resv = 0; 164530925561SChris Williamson 164630925561SChris Williamson (void) snprintf(errbuf, sizeof (errbuf), 164730925561SChris Williamson dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 164830925561SChris Williamson zhp->zfs_name); 164930925561SChris Williamson 165030925561SChris Williamson if ((nvl = zfs_valid_proplist(hdl, zhp->zfs_type, props, 1651e9316f76SJoe Stein zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, zhp->zpool_hdl, 1652e9316f76SJoe Stein errbuf)) == NULL) 1653e9dbad6fSeschrock goto error; 1654990b4856Slling 165530925561SChris Williamson /* 165630925561SChris Williamson * We have to check for any extra properties which need to be added 165730925561SChris Williamson * before computing the length of the nvlist. 165830925561SChris Williamson */ 165930925561SChris Williamson for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL); 166030925561SChris Williamson elem != NULL; 166130925561SChris Williamson elem = nvlist_next_nvpair(nvl, elem)) { 166230925561SChris Williamson if (zfs_name_to_prop(nvpair_name(elem)) == ZFS_PROP_VOLSIZE && 166330925561SChris Williamson (added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1) { 166430925561SChris Williamson goto error; 166530925561SChris Williamson } 166630925561SChris Williamson } 166730925561SChris Williamson /* 166830925561SChris Williamson * Check how many properties we're setting and allocate an array to 166930925561SChris Williamson * store changelist pointers for postfix(). 167030925561SChris Williamson */ 167130925561SChris Williamson nvl_len = 0; 167230925561SChris Williamson for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL); 167330925561SChris Williamson elem != NULL; 167430925561SChris Williamson elem = nvlist_next_nvpair(nvl, elem)) 167530925561SChris Williamson nvl_len++; 167630925561SChris Williamson if ((cls = calloc(nvl_len, sizeof (prop_changelist_t *))) == NULL) 167730925561SChris Williamson goto error; 1678e9dbad6fSeschrock 167930925561SChris Williamson cl_idx = 0; 168030925561SChris Williamson for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL); 168130925561SChris Williamson elem != NULL; 168230925561SChris Williamson elem = nvlist_next_nvpair(nvl, elem)) { 1683e9dbad6fSeschrock 168430925561SChris Williamson zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem)); 168530925561SChris Williamson 168630925561SChris Williamson assert(cl_idx < nvl_len); 168730925561SChris Williamson /* 168830925561SChris Williamson * We don't want to unmount & remount the dataset when changing 168930925561SChris Williamson * its canmount property to 'on' or 'noauto'. We only use 169030925561SChris Williamson * the changelist logic to unmount when setting canmount=off. 169130925561SChris Williamson */ 1692c079fa4dSAndriy Gapon if (prop != ZFS_PROP_CANMOUNT || 1693c079fa4dSAndriy Gapon (fnvpair_value_uint64(elem) == ZFS_CANMOUNT_OFF && 1694c079fa4dSAndriy Gapon zfs_is_mounted(zhp, NULL))) { 169530925561SChris Williamson cls[cl_idx] = changelist_gather(zhp, prop, 0, 0); 169630925561SChris Williamson if (cls[cl_idx] == NULL) 169736db6475SEric Taylor goto error; 169836db6475SEric Taylor } 169936db6475SEric Taylor 170030925561SChris Williamson if (prop == ZFS_PROP_MOUNTPOINT && 170130925561SChris Williamson changelist_haszonedchild(cls[cl_idx])) { 170299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1703fa9e4066Sahrens "child dataset with inherited mountpoint is used " 170499653d4eSeschrock "in a non-global zone")); 170599653d4eSeschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1706fa9e4066Sahrens goto error; 1707fa9e4066Sahrens } 1708fa9e4066Sahrens 170930925561SChris Williamson if (cls[cl_idx] != NULL && 171030925561SChris Williamson (ret = changelist_prefix(cls[cl_idx])) != 0) 1711fa9e4066Sahrens goto error; 1712fa9e4066Sahrens 171330925561SChris Williamson cl_idx++; 171430925561SChris Williamson } 171530925561SChris Williamson assert(cl_idx == nvl_len); 171630925561SChris Williamson 1717fa9e4066Sahrens /* 171830925561SChris Williamson * Execute the corresponding ioctl() to set this list of properties. 1719fa9e4066Sahrens */ 1720fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1721fa9e4066Sahrens 172230925561SChris Williamson if ((ret = zcmd_write_src_nvlist(hdl, &zc, nvl)) != 0 || 172330925561SChris Williamson (ret = zcmd_alloc_dst_nvlist(hdl, &zc, 0)) != 0) 1724e9dbad6fSeschrock goto error; 1725e9dbad6fSeschrock 1726ecd6cf80Smarks ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc); 1727743a77edSAlan Wright 1728fa9e4066Sahrens if (ret != 0) { 172930925561SChris Williamson /* Get the list of unset properties back and report them. */ 173030925561SChris Williamson nvlist_t *errorprops = NULL; 173130925561SChris Williamson if (zcmd_read_dst_nvlist(hdl, &zc, &errorprops) != 0) 173230925561SChris Williamson goto error; 173330925561SChris Williamson for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL); 173430925561SChris Williamson elem != NULL; 173530925561SChris Williamson elem = nvlist_next_nvpair(nvl, elem)) { 173630925561SChris Williamson zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem)); 173792241e0bSTom Erickson zfs_setprop_error(hdl, prop, errno, errbuf); 173830925561SChris Williamson } 173930925561SChris Williamson nvlist_free(errorprops); 174030925561SChris Williamson 174136db6475SEric Taylor if (added_resv && errno == ENOSPC) { 174236db6475SEric Taylor /* clean up the volsize property we tried to set */ 174336db6475SEric Taylor uint64_t old_volsize = zfs_prop_get_int(zhp, 174436db6475SEric Taylor ZFS_PROP_VOLSIZE); 174536db6475SEric Taylor nvlist_free(nvl); 174630925561SChris Williamson nvl = NULL; 174736db6475SEric Taylor zcmd_free_nvlists(&zc); 174830925561SChris Williamson 174936db6475SEric Taylor if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) 175036db6475SEric Taylor goto error; 175136db6475SEric Taylor if (nvlist_add_uint64(nvl, 175236db6475SEric Taylor zfs_prop_to_name(ZFS_PROP_VOLSIZE), 175336db6475SEric Taylor old_volsize) != 0) 175436db6475SEric Taylor goto error; 175536db6475SEric Taylor if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0) 175636db6475SEric Taylor goto error; 175736db6475SEric Taylor (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc); 175836db6475SEric Taylor } 1759fa9e4066Sahrens } else { 176030925561SChris Williamson for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) { 176130925561SChris Williamson if (cls[cl_idx] != NULL) { 176230925561SChris Williamson int clp_err = changelist_postfix(cls[cl_idx]); 176330925561SChris Williamson if (clp_err != 0) 176430925561SChris Williamson ret = clp_err; 176530925561SChris Williamson } 176630925561SChris Williamson } 1767a227b7f4Shs24103 1768fa9e4066Sahrens /* 1769fa9e4066Sahrens * Refresh the statistics so the new property value 1770fa9e4066Sahrens * is reflected. 1771fa9e4066Sahrens */ 1772a227b7f4Shs24103 if (ret == 0) 1773fa9e4066Sahrens (void) get_stats(zhp); 1774fa9e4066Sahrens } 1775fa9e4066Sahrens 1776fa9e4066Sahrens error: 1777e9dbad6fSeschrock nvlist_free(nvl); 1778e9dbad6fSeschrock zcmd_free_nvlists(&zc); 177930925561SChris Williamson if (cls != NULL) { 178030925561SChris Williamson for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) { 178130925561SChris Williamson if (cls[cl_idx] != NULL) 178230925561SChris Williamson changelist_free(cls[cl_idx]); 178330925561SChris Williamson } 178430925561SChris Williamson free(cls); 178530925561SChris Williamson } 1786fa9e4066Sahrens return (ret); 1787fa9e4066Sahrens } 1788fa9e4066Sahrens 1789fa9e4066Sahrens /* 179092241e0bSTom Erickson * Given a property, inherit the value from the parent dataset, or if received 179192241e0bSTom Erickson * is TRUE, revert to the received value, if any. 1792fa9e4066Sahrens */ 1793fa9e4066Sahrens int 179492241e0bSTom Erickson zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received) 1795fa9e4066Sahrens { 1796fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 1797fa9e4066Sahrens int ret; 1798fa9e4066Sahrens prop_changelist_t *cl; 179999653d4eSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 180099653d4eSeschrock char errbuf[1024]; 1801e9dbad6fSeschrock zfs_prop_t prop; 180299653d4eSeschrock 180399653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 180499653d4eSeschrock "cannot inherit %s for '%s'"), propname, zhp->zfs_name); 1805fa9e4066Sahrens 180692241e0bSTom Erickson zc.zc_cookie = received; 1807990b4856Slling if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) { 1808e9dbad6fSeschrock /* 1809e9dbad6fSeschrock * For user properties, the amount of work we have to do is very 1810e9dbad6fSeschrock * small, so just do it here. 1811e9dbad6fSeschrock */ 1812e9dbad6fSeschrock if (!zfs_prop_user(propname)) { 1813e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1814e9dbad6fSeschrock "invalid property")); 1815e9dbad6fSeschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 1816e9dbad6fSeschrock } 1817e9dbad6fSeschrock 1818e9dbad6fSeschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1819e9dbad6fSeschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 1820e9dbad6fSeschrock 1821e45ce728Sahrens if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0) 1822e9dbad6fSeschrock return (zfs_standard_error(hdl, errno, errbuf)); 1823e9dbad6fSeschrock 1824e9dbad6fSeschrock return (0); 1825e9dbad6fSeschrock } 1826e9dbad6fSeschrock 1827fa9e4066Sahrens /* 1828fa9e4066Sahrens * Verify that this property is inheritable. 1829fa9e4066Sahrens */ 183099653d4eSeschrock if (zfs_prop_readonly(prop)) 183199653d4eSeschrock return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf)); 1832fa9e4066Sahrens 183392241e0bSTom Erickson if (!zfs_prop_inheritable(prop) && !received) 183499653d4eSeschrock return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf)); 1835fa9e4066Sahrens 1836fa9e4066Sahrens /* 1837fa9e4066Sahrens * Check to see if the value applies to this type 1838fa9e4066Sahrens */ 183999653d4eSeschrock if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 184099653d4eSeschrock return (zfs_error(hdl, EZFS_PROPTYPE, errbuf)); 1841fa9e4066Sahrens 1842bf7c2d40Srm160521 /* 184336db6475SEric Taylor * Normalize the name, to get rid of shorthand abbreviations. 1844bf7c2d40Srm160521 */ 1845bf7c2d40Srm160521 propname = zfs_prop_to_name(prop); 1846fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1847e9dbad6fSeschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 1848fa9e4066Sahrens 1849fa9e4066Sahrens if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID && 1850fa9e4066Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 185199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 185299653d4eSeschrock "dataset is used in a non-global zone")); 185399653d4eSeschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 1854fa9e4066Sahrens } 1855fa9e4066Sahrens 1856fa9e4066Sahrens /* 1857fa9e4066Sahrens * Determine datasets which will be affected by this change, if any. 1858fa9e4066Sahrens */ 18590069fd67STim Haley if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL) 1860fa9e4066Sahrens return (-1); 1861fa9e4066Sahrens 1862fa9e4066Sahrens if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 186399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 186499653d4eSeschrock "child dataset with inherited mountpoint is used " 186599653d4eSeschrock "in a non-global zone")); 186699653d4eSeschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1867fa9e4066Sahrens goto error; 1868fa9e4066Sahrens } 1869fa9e4066Sahrens 1870fa9e4066Sahrens if ((ret = changelist_prefix(cl)) != 0) 1871fa9e4066Sahrens goto error; 1872fa9e4066Sahrens 1873e45ce728Sahrens if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) { 187499653d4eSeschrock return (zfs_standard_error(hdl, errno, errbuf)); 1875fa9e4066Sahrens } else { 1876fa9e4066Sahrens 1877efc555ebSnd150628 if ((ret = changelist_postfix(cl)) != 0) 1878fa9e4066Sahrens goto error; 1879fa9e4066Sahrens 1880fa9e4066Sahrens /* 1881fa9e4066Sahrens * Refresh the statistics so the new property is reflected. 1882fa9e4066Sahrens */ 1883fa9e4066Sahrens (void) get_stats(zhp); 1884fa9e4066Sahrens } 1885fa9e4066Sahrens 1886fa9e4066Sahrens error: 1887fa9e4066Sahrens changelist_free(cl); 1888fa9e4066Sahrens return (ret); 1889fa9e4066Sahrens } 1890fa9e4066Sahrens 1891fa9e4066Sahrens /* 18927f7322feSeschrock * True DSL properties are stored in an nvlist. The following two functions 18937f7322feSeschrock * extract them appropriately. 18947f7322feSeschrock */ 18957f7322feSeschrock static uint64_t 18967f7322feSeschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 18977f7322feSeschrock { 18987f7322feSeschrock nvlist_t *nv; 18997f7322feSeschrock uint64_t value; 19007f7322feSeschrock 1901a2eea2e1Sahrens *source = NULL; 19027f7322feSeschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 19037f7322feSeschrock zfs_prop_to_name(prop), &nv) == 0) { 1904990b4856Slling verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0); 1905990b4856Slling (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 19067f7322feSeschrock } else { 19072e5e9e19SSanjeev Bagewadi verify(!zhp->zfs_props_table || 19082e5e9e19SSanjeev Bagewadi zhp->zfs_props_table[prop] == B_TRUE); 19097f7322feSeschrock value = zfs_prop_default_numeric(prop); 19107f7322feSeschrock *source = ""; 19117f7322feSeschrock } 19127f7322feSeschrock 19137f7322feSeschrock return (value); 19147f7322feSeschrock } 19157f7322feSeschrock 19169c3fd121SMatthew Ahrens static const char * 19177f7322feSeschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 19187f7322feSeschrock { 19197f7322feSeschrock nvlist_t *nv; 19209c3fd121SMatthew Ahrens const char *value; 19217f7322feSeschrock 1922a2eea2e1Sahrens *source = NULL; 19237f7322feSeschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 19247f7322feSeschrock zfs_prop_to_name(prop), &nv) == 0) { 19259c3fd121SMatthew Ahrens value = fnvlist_lookup_string(nv, ZPROP_VALUE); 1926990b4856Slling (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 19277f7322feSeschrock } else { 19282e5e9e19SSanjeev Bagewadi verify(!zhp->zfs_props_table || 19292e5e9e19SSanjeev Bagewadi zhp->zfs_props_table[prop] == B_TRUE); 19309c3fd121SMatthew Ahrens value = zfs_prop_default_string(prop); 19317f7322feSeschrock *source = ""; 19327f7322feSeschrock } 19337f7322feSeschrock 19347f7322feSeschrock return (value); 19357f7322feSeschrock } 19367f7322feSeschrock 193792241e0bSTom Erickson static boolean_t 193892241e0bSTom Erickson zfs_is_recvd_props_mode(zfs_handle_t *zhp) 193992241e0bSTom Erickson { 194092241e0bSTom Erickson return (zhp->zfs_props == zhp->zfs_recvd_props); 194192241e0bSTom Erickson } 194292241e0bSTom Erickson 194392241e0bSTom Erickson static void 194492241e0bSTom Erickson zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie) 194592241e0bSTom Erickson { 194692241e0bSTom Erickson *cookie = (uint64_t)(uintptr_t)zhp->zfs_props; 194792241e0bSTom Erickson zhp->zfs_props = zhp->zfs_recvd_props; 194892241e0bSTom Erickson } 194992241e0bSTom Erickson 195092241e0bSTom Erickson static void 195192241e0bSTom Erickson zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie) 195292241e0bSTom Erickson { 195392241e0bSTom Erickson zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie; 195492241e0bSTom Erickson *cookie = 0; 195592241e0bSTom Erickson } 195692241e0bSTom Erickson 19577f7322feSeschrock /* 1958fa9e4066Sahrens * Internal function for getting a numeric property. Both zfs_prop_get() and 1959fa9e4066Sahrens * zfs_prop_get_int() are built using this interface. 1960fa9e4066Sahrens * 1961fa9e4066Sahrens * Certain properties can be overridden using 'mount -o'. In this case, scan 1962fa9e4066Sahrens * the contents of the /etc/mnttab entry, searching for the appropriate options. 1963fa9e4066Sahrens * If they differ from the on-disk values, report the current values and mark 1964fa9e4066Sahrens * the source "temporary". 1965fa9e4066Sahrens */ 196699653d4eSeschrock static int 1967990b4856Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src, 196899653d4eSeschrock char **source, uint64_t *val) 1969fa9e4066Sahrens { 1970bd00f61bSrm160521 zfs_cmd_t zc = { 0 }; 197196510749Stimh nvlist_t *zplprops = NULL; 1972fa9e4066Sahrens struct mnttab mnt; 19733ccfa83cSahrens char *mntopt_on = NULL; 19743ccfa83cSahrens char *mntopt_off = NULL; 197592241e0bSTom Erickson boolean_t received = zfs_is_recvd_props_mode(zhp); 1976fa9e4066Sahrens 1977fa9e4066Sahrens *source = NULL; 1978fa9e4066Sahrens 19793ccfa83cSahrens switch (prop) { 19803ccfa83cSahrens case ZFS_PROP_ATIME: 19813ccfa83cSahrens mntopt_on = MNTOPT_ATIME; 19823ccfa83cSahrens mntopt_off = MNTOPT_NOATIME; 19833ccfa83cSahrens break; 19843ccfa83cSahrens 19853ccfa83cSahrens case ZFS_PROP_DEVICES: 19863ccfa83cSahrens mntopt_on = MNTOPT_DEVICES; 19873ccfa83cSahrens mntopt_off = MNTOPT_NODEVICES; 19883ccfa83cSahrens break; 19893ccfa83cSahrens 19903ccfa83cSahrens case ZFS_PROP_EXEC: 19913ccfa83cSahrens mntopt_on = MNTOPT_EXEC; 19923ccfa83cSahrens mntopt_off = MNTOPT_NOEXEC; 19933ccfa83cSahrens break; 19943ccfa83cSahrens 19953ccfa83cSahrens case ZFS_PROP_READONLY: 19963ccfa83cSahrens mntopt_on = MNTOPT_RO; 19973ccfa83cSahrens mntopt_off = MNTOPT_RW; 19983ccfa83cSahrens break; 19993ccfa83cSahrens 20003ccfa83cSahrens case ZFS_PROP_SETUID: 20013ccfa83cSahrens mntopt_on = MNTOPT_SETUID; 20023ccfa83cSahrens mntopt_off = MNTOPT_NOSETUID; 20033ccfa83cSahrens break; 20043ccfa83cSahrens 20053ccfa83cSahrens case ZFS_PROP_XATTR: 20063ccfa83cSahrens mntopt_on = MNTOPT_XATTR; 20073ccfa83cSahrens mntopt_off = MNTOPT_NOXATTR; 20083ccfa83cSahrens break; 2009da6c28aaSamw 2010da6c28aaSamw case ZFS_PROP_NBMAND: 2011da6c28aaSamw mntopt_on = MNTOPT_NBMAND; 2012da6c28aaSamw mntopt_off = MNTOPT_NONBMAND; 2013da6c28aaSamw break; 201488f61deeSIgor Kozhukhov 201588f61deeSIgor Kozhukhov default: 201688f61deeSIgor Kozhukhov break; 20173ccfa83cSahrens } 20183ccfa83cSahrens 20193bb79becSeschrock /* 20203bb79becSeschrock * Because looking up the mount options is potentially expensive 20213bb79becSeschrock * (iterating over all of /etc/mnttab), we defer its calculation until 20223bb79becSeschrock * we're looking up a property which requires its presence. 20233bb79becSeschrock */ 20243bb79becSeschrock if (!zhp->zfs_mntcheck && 20253ccfa83cSahrens (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) { 2026ebedde84SEric Taylor libzfs_handle_t *hdl = zhp->zfs_hdl; 2027ebedde84SEric Taylor struct mnttab entry; 20283bb79becSeschrock 2029ebedde84SEric Taylor if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) { 2030ebedde84SEric Taylor zhp->zfs_mntopts = zfs_strdup(hdl, 20313ccfa83cSahrens entry.mnt_mntopts); 20323ccfa83cSahrens if (zhp->zfs_mntopts == NULL) 20333bb79becSeschrock return (-1); 20343ccfa83cSahrens } 20353bb79becSeschrock 20363bb79becSeschrock zhp->zfs_mntcheck = B_TRUE; 20373bb79becSeschrock } 20383bb79becSeschrock 2039fa9e4066Sahrens if (zhp->zfs_mntopts == NULL) 2040fa9e4066Sahrens mnt.mnt_mntopts = ""; 2041fa9e4066Sahrens else 2042fa9e4066Sahrens mnt.mnt_mntopts = zhp->zfs_mntopts; 2043fa9e4066Sahrens 2044fa9e4066Sahrens switch (prop) { 2045fa9e4066Sahrens case ZFS_PROP_ATIME: 2046fa9e4066Sahrens case ZFS_PROP_DEVICES: 2047fa9e4066Sahrens case ZFS_PROP_EXEC: 20483ccfa83cSahrens case ZFS_PROP_READONLY: 20493ccfa83cSahrens case ZFS_PROP_SETUID: 20503ccfa83cSahrens case ZFS_PROP_XATTR: 2051da6c28aaSamw case ZFS_PROP_NBMAND: 205299653d4eSeschrock *val = getprop_uint64(zhp, prop, source); 2053fa9e4066Sahrens 205492241e0bSTom Erickson if (received) 205592241e0bSTom Erickson break; 205692241e0bSTom Erickson 20573ccfa83cSahrens if (hasmntopt(&mnt, mntopt_on) && !*val) { 205899653d4eSeschrock *val = B_TRUE; 2059fa9e4066Sahrens if (src) 2060990b4856Slling *src = ZPROP_SRC_TEMPORARY; 20613ccfa83cSahrens } else if (hasmntopt(&mnt, mntopt_off) && *val) { 206299653d4eSeschrock *val = B_FALSE; 2063fa9e4066Sahrens if (src) 2064990b4856Slling *src = ZPROP_SRC_TEMPORARY; 2065fa9e4066Sahrens } 206699653d4eSeschrock break; 2067fa9e4066Sahrens 20683ccfa83cSahrens case ZFS_PROP_CANMOUNT: 2069d41c4376SMark J Musante case ZFS_PROP_VOLSIZE: 2070fa9e4066Sahrens case ZFS_PROP_QUOTA: 2071a9799022Sck153898 case ZFS_PROP_REFQUOTA: 2072fa9e4066Sahrens case ZFS_PROP_RESERVATION: 2073a9799022Sck153898 case ZFS_PROP_REFRESERVATION: 2074a2afb611SJerry Jelinek case ZFS_PROP_FILESYSTEM_LIMIT: 2075a2afb611SJerry Jelinek case ZFS_PROP_SNAPSHOT_LIMIT: 2076a2afb611SJerry Jelinek case ZFS_PROP_FILESYSTEM_COUNT: 2077a2afb611SJerry Jelinek case ZFS_PROP_SNAPSHOT_COUNT: 2078a2eea2e1Sahrens *val = getprop_uint64(zhp, prop, source); 207992241e0bSTom Erickson 208092241e0bSTom Erickson if (*source == NULL) { 208192241e0bSTom Erickson /* not default, must be local */ 2082fa9e4066Sahrens *source = zhp->zfs_name; 208392241e0bSTom Erickson } 208499653d4eSeschrock break; 2085fa9e4066Sahrens 2086fa9e4066Sahrens case ZFS_PROP_MOUNTED: 208799653d4eSeschrock *val = (zhp->zfs_mntopts != NULL); 208899653d4eSeschrock break; 2089fa9e4066Sahrens 209039c23413Seschrock case ZFS_PROP_NUMCLONES: 209139c23413Seschrock *val = zhp->zfs_dmustats.dds_num_clones; 209239c23413Seschrock break; 209339c23413Seschrock 2094bd00f61bSrm160521 case ZFS_PROP_VERSION: 2095de8267e0Stimh case ZFS_PROP_NORMALIZE: 2096de8267e0Stimh case ZFS_PROP_UTF8ONLY: 2097de8267e0Stimh case ZFS_PROP_CASE: 2098de8267e0Stimh if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) || 2099de8267e0Stimh zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 21003d7934e1Srm160521 return (-1); 2101bd00f61bSrm160521 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2102de8267e0Stimh if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) { 2103de8267e0Stimh zcmd_free_nvlists(&zc); 2104f4b94bdeSMatthew Ahrens return (-1); 2105bd00f61bSrm160521 } 2106de8267e0Stimh if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 || 2107de8267e0Stimh nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop), 2108de8267e0Stimh val) != 0) { 2109de8267e0Stimh zcmd_free_nvlists(&zc); 2110f4b94bdeSMatthew Ahrens return (-1); 2111de8267e0Stimh } 211296510749Stimh nvlist_free(zplprops); 2113de8267e0Stimh zcmd_free_nvlists(&zc); 2114bd00f61bSrm160521 break; 2115bd00f61bSrm160521 2116ca48f36fSKeith M Wesolowski case ZFS_PROP_INCONSISTENT: 2117ca48f36fSKeith M Wesolowski *val = zhp->zfs_dmustats.dds_inconsistent; 2118ca48f36fSKeith M Wesolowski break; 2119ca48f36fSKeith M Wesolowski 2120fa9e4066Sahrens default: 2121e7437265Sahrens switch (zfs_prop_get_type(prop)) { 212291ebeef5Sahrens case PROP_TYPE_NUMBER: 212391ebeef5Sahrens case PROP_TYPE_INDEX: 2124e7437265Sahrens *val = getprop_uint64(zhp, prop, source); 212574e7dc98SMatthew Ahrens /* 212614843421SMatthew Ahrens * If we tried to use a default value for a 212774e7dc98SMatthew Ahrens * readonly property, it means that it was not 21284d86c0eaSMatthew Ahrens * present. Note this only applies to "truly" 21294d86c0eaSMatthew Ahrens * readonly properties, not set-once properties 21304d86c0eaSMatthew Ahrens * like volblocksize. 213174e7dc98SMatthew Ahrens */ 213274e7dc98SMatthew Ahrens if (zfs_prop_readonly(prop) && 21334d86c0eaSMatthew Ahrens !zfs_prop_setonce(prop) && 213431f572c2STom Erickson *source != NULL && (*source)[0] == '\0') { 213531f572c2STom Erickson *source = NULL; 2136ad2760acSMatthew Ahrens return (-1); 213774e7dc98SMatthew Ahrens } 2138e7437265Sahrens break; 2139e7437265Sahrens 214091ebeef5Sahrens case PROP_TYPE_STRING: 2141e7437265Sahrens default: 214299653d4eSeschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 214399653d4eSeschrock "cannot get non-numeric property")); 214499653d4eSeschrock return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP, 214599653d4eSeschrock dgettext(TEXT_DOMAIN, "internal error"))); 2146fa9e4066Sahrens } 2147e7437265Sahrens } 2148fa9e4066Sahrens 2149fa9e4066Sahrens return (0); 2150fa9e4066Sahrens } 2151fa9e4066Sahrens 2152fa9e4066Sahrens /* 2153fa9e4066Sahrens * Calculate the source type, given the raw source string. 2154fa9e4066Sahrens */ 2155fa9e4066Sahrens static void 2156990b4856Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source, 2157fa9e4066Sahrens char *statbuf, size_t statlen) 2158fa9e4066Sahrens { 2159990b4856Slling if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY) 2160fa9e4066Sahrens return; 2161fa9e4066Sahrens 2162fa9e4066Sahrens if (source == NULL) { 2163990b4856Slling *srctype = ZPROP_SRC_NONE; 2164fa9e4066Sahrens } else if (source[0] == '\0') { 2165990b4856Slling *srctype = ZPROP_SRC_DEFAULT; 216692241e0bSTom Erickson } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) { 216792241e0bSTom Erickson *srctype = ZPROP_SRC_RECEIVED; 2168fa9e4066Sahrens } else { 2169fa9e4066Sahrens if (strcmp(source, zhp->zfs_name) == 0) { 2170990b4856Slling *srctype = ZPROP_SRC_LOCAL; 2171fa9e4066Sahrens } else { 2172fa9e4066Sahrens (void) strlcpy(statbuf, source, statlen); 2173990b4856Slling *srctype = ZPROP_SRC_INHERITED; 2174fa9e4066Sahrens } 2175fa9e4066Sahrens } 2176fa9e4066Sahrens 2177fa9e4066Sahrens } 2178fa9e4066Sahrens 217992241e0bSTom Erickson int 218092241e0bSTom Erickson zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf, 218192241e0bSTom Erickson size_t proplen, boolean_t literal) 218292241e0bSTom Erickson { 218392241e0bSTom Erickson zfs_prop_t prop; 218492241e0bSTom Erickson int err = 0; 218592241e0bSTom Erickson 218692241e0bSTom Erickson if (zhp->zfs_recvd_props == NULL) 218792241e0bSTom Erickson if (get_recvd_props_ioctl(zhp) != 0) 218892241e0bSTom Erickson return (-1); 218992241e0bSTom Erickson 219092241e0bSTom Erickson prop = zfs_name_to_prop(propname); 219192241e0bSTom Erickson 219292241e0bSTom Erickson if (prop != ZPROP_INVAL) { 219392241e0bSTom Erickson uint64_t cookie; 219492241e0bSTom Erickson if (!nvlist_exists(zhp->zfs_recvd_props, propname)) 219592241e0bSTom Erickson return (-1); 219692241e0bSTom Erickson zfs_set_recvd_props_mode(zhp, &cookie); 219792241e0bSTom Erickson err = zfs_prop_get(zhp, prop, propbuf, proplen, 219892241e0bSTom Erickson NULL, NULL, 0, literal); 219992241e0bSTom Erickson zfs_unset_recvd_props_mode(zhp, &cookie); 220092241e0bSTom Erickson } else { 220192241e0bSTom Erickson nvlist_t *propval; 220292241e0bSTom Erickson char *recvdval; 220392241e0bSTom Erickson if (nvlist_lookup_nvlist(zhp->zfs_recvd_props, 220492241e0bSTom Erickson propname, &propval) != 0) 220592241e0bSTom Erickson return (-1); 220692241e0bSTom Erickson verify(nvlist_lookup_string(propval, ZPROP_VALUE, 220792241e0bSTom Erickson &recvdval) == 0); 220892241e0bSTom Erickson (void) strlcpy(propbuf, recvdval, proplen); 220992241e0bSTom Erickson } 221092241e0bSTom Erickson 221192241e0bSTom Erickson return (err == 0 ? 0 : -1); 221292241e0bSTom Erickson } 221392241e0bSTom Erickson 221419b94df9SMatthew Ahrens static int 221519b94df9SMatthew Ahrens get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen) 221619b94df9SMatthew Ahrens { 221719b94df9SMatthew Ahrens nvlist_t *value; 221819b94df9SMatthew Ahrens nvpair_t *pair; 221919b94df9SMatthew Ahrens 222019b94df9SMatthew Ahrens value = zfs_get_clones_nvl(zhp); 222119b94df9SMatthew Ahrens if (value == NULL) 222219b94df9SMatthew Ahrens return (-1); 222319b94df9SMatthew Ahrens 222419b94df9SMatthew Ahrens propbuf[0] = '\0'; 222519b94df9SMatthew Ahrens for (pair = nvlist_next_nvpair(value, NULL); pair != NULL; 222619b94df9SMatthew Ahrens pair = nvlist_next_nvpair(value, pair)) { 222719b94df9SMatthew Ahrens if (propbuf[0] != '\0') 222819b94df9SMatthew Ahrens (void) strlcat(propbuf, ",", proplen); 222919b94df9SMatthew Ahrens (void) strlcat(propbuf, nvpair_name(pair), proplen); 223019b94df9SMatthew Ahrens } 223119b94df9SMatthew Ahrens 223219b94df9SMatthew Ahrens return (0); 223319b94df9SMatthew Ahrens } 223419b94df9SMatthew Ahrens 223519b94df9SMatthew Ahrens struct get_clones_arg { 223619b94df9SMatthew Ahrens uint64_t numclones; 223719b94df9SMatthew Ahrens nvlist_t *value; 223819b94df9SMatthew Ahrens const char *origin; 22399adfa60dSMatthew Ahrens char buf[ZFS_MAX_DATASET_NAME_LEN]; 224019b94df9SMatthew Ahrens }; 224119b94df9SMatthew Ahrens 224219b94df9SMatthew Ahrens int 224319b94df9SMatthew Ahrens get_clones_cb(zfs_handle_t *zhp, void *arg) 224419b94df9SMatthew Ahrens { 224519b94df9SMatthew Ahrens struct get_clones_arg *gca = arg; 224619b94df9SMatthew Ahrens 224719b94df9SMatthew Ahrens if (gca->numclones == 0) { 224819b94df9SMatthew Ahrens zfs_close(zhp); 224919b94df9SMatthew Ahrens return (0); 225019b94df9SMatthew Ahrens } 225119b94df9SMatthew Ahrens 225219b94df9SMatthew Ahrens if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf), 225319b94df9SMatthew Ahrens NULL, NULL, 0, B_TRUE) != 0) 225419b94df9SMatthew Ahrens goto out; 225519b94df9SMatthew Ahrens if (strcmp(gca->buf, gca->origin) == 0) { 22563b2aab18SMatthew Ahrens fnvlist_add_boolean(gca->value, zfs_get_name(zhp)); 225719b94df9SMatthew Ahrens gca->numclones--; 225819b94df9SMatthew Ahrens } 225919b94df9SMatthew Ahrens 226019b94df9SMatthew Ahrens out: 226119b94df9SMatthew Ahrens (void) zfs_iter_children(zhp, get_clones_cb, gca); 226219b94df9SMatthew Ahrens zfs_close(zhp); 226319b94df9SMatthew Ahrens return (0); 226419b94df9SMatthew Ahrens } 226519b94df9SMatthew Ahrens 226619b94df9SMatthew Ahrens nvlist_t * 226719b94df9SMatthew Ahrens zfs_get_clones_nvl(zfs_handle_t *zhp) 226819b94df9SMatthew Ahrens { 226919b94df9SMatthew Ahrens nvlist_t *nv, *value; 227019b94df9SMatthew Ahrens 227119b94df9SMatthew Ahrens if (nvlist_lookup_nvlist(zhp->zfs_props, 227219b94df9SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) { 227319b94df9SMatthew Ahrens struct get_clones_arg gca; 227419b94df9SMatthew Ahrens 227519b94df9SMatthew Ahrens /* 227619b94df9SMatthew Ahrens * if this is a snapshot, then the kernel wasn't able 227719b94df9SMatthew Ahrens * to get the clones. Do it by slowly iterating. 227819b94df9SMatthew Ahrens */ 227919b94df9SMatthew Ahrens if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) 228019b94df9SMatthew Ahrens return (NULL); 228119b94df9SMatthew Ahrens if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0) 228219b94df9SMatthew Ahrens return (NULL); 228319b94df9SMatthew Ahrens if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) { 228419b94df9SMatthew Ahrens nvlist_free(nv); 228519b94df9SMatthew Ahrens return (NULL); 228619b94df9SMatthew Ahrens } 228719b94df9SMatthew Ahrens 228819b94df9SMatthew Ahrens gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES); 228919b94df9SMatthew Ahrens gca.value = value; 229019b94df9SMatthew Ahrens gca.origin = zhp->zfs_name; 229119b94df9SMatthew Ahrens 229219b94df9SMatthew Ahrens if (gca.numclones != 0) { 229319b94df9SMatthew Ahrens zfs_handle_t *root; 22949adfa60dSMatthew Ahrens char pool[ZFS_MAX_DATASET_NAME_LEN]; 229519b94df9SMatthew Ahrens char *cp = pool; 229619b94df9SMatthew Ahrens 229719b94df9SMatthew Ahrens /* get the pool name */ 229819b94df9SMatthew Ahrens (void) strlcpy(pool, zhp->zfs_name, sizeof (pool)); 229919b94df9SMatthew Ahrens (void) strsep(&cp, "/@"); 230019b94df9SMatthew Ahrens root = zfs_open(zhp->zfs_hdl, pool, 230119b94df9SMatthew Ahrens ZFS_TYPE_FILESYSTEM); 230219b94df9SMatthew Ahrens 230319b94df9SMatthew Ahrens (void) get_clones_cb(root, &gca); 230419b94df9SMatthew Ahrens } 230519b94df9SMatthew Ahrens 230619b94df9SMatthew Ahrens if (gca.numclones != 0 || 230719b94df9SMatthew Ahrens nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 || 230819b94df9SMatthew Ahrens nvlist_add_nvlist(zhp->zfs_props, 230919b94df9SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) { 231019b94df9SMatthew Ahrens nvlist_free(nv); 231119b94df9SMatthew Ahrens nvlist_free(value); 231219b94df9SMatthew Ahrens return (NULL); 231319b94df9SMatthew Ahrens } 231419b94df9SMatthew Ahrens nvlist_free(nv); 231519b94df9SMatthew Ahrens nvlist_free(value); 231619b94df9SMatthew Ahrens verify(0 == nvlist_lookup_nvlist(zhp->zfs_props, 231719b94df9SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_CLONES), &nv)); 231819b94df9SMatthew Ahrens } 231919b94df9SMatthew Ahrens 232019b94df9SMatthew Ahrens verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0); 232119b94df9SMatthew Ahrens 232219b94df9SMatthew Ahrens return (value); 232319b94df9SMatthew Ahrens } 232419b94df9SMatthew Ahrens 2325fa9e4066Sahrens /* 2326fa9e4066Sahrens * Retrieve a property from the given object. If 'literal' is specified, then 2327fa9e4066Sahrens * numbers are left as exact values. Otherwise, numbers are converted to a 2328fa9e4066Sahrens * human-readable form. 2329fa9e4066Sahrens * 2330fa9e4066Sahrens * Returns 0 on success, or -1 on error. 2331fa9e4066Sahrens */ 2332fa9e4066Sahrens int 2333fa9e4066Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen, 2334990b4856Slling zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal) 2335fa9e4066Sahrens { 2336fa9e4066Sahrens char *source = NULL; 2337fa9e4066Sahrens uint64_t val; 23389c3fd121SMatthew Ahrens const char *str; 2339e9dbad6fSeschrock const char *strval; 234092241e0bSTom Erickson boolean_t received = zfs_is_recvd_props_mode(zhp); 2341fa9e4066Sahrens 2342fa9e4066Sahrens /* 2343fa9e4066Sahrens * Check to see if this property applies to our object 2344fa9e4066Sahrens */ 2345fa9e4066Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 2346fa9e4066Sahrens return (-1); 2347fa9e4066Sahrens 234892241e0bSTom Erickson if (received && zfs_prop_readonly(prop)) 234992241e0bSTom Erickson return (-1); 235092241e0bSTom Erickson 2351fa9e4066Sahrens if (src) 2352990b4856Slling *src = ZPROP_SRC_NONE; 2353fa9e4066Sahrens 2354fa9e4066Sahrens switch (prop) { 2355fa9e4066Sahrens case ZFS_PROP_CREATION: 2356fa9e4066Sahrens /* 2357fa9e4066Sahrens * 'creation' is a time_t stored in the statistics. We convert 2358fa9e4066Sahrens * this into a string unless 'literal' is specified. 2359fa9e4066Sahrens */ 2360fa9e4066Sahrens { 2361a2eea2e1Sahrens val = getprop_uint64(zhp, prop, &source); 2362a2eea2e1Sahrens time_t time = (time_t)val; 2363fa9e4066Sahrens struct tm t; 2364fa9e4066Sahrens 2365fa9e4066Sahrens if (literal || 2366fa9e4066Sahrens localtime_r(&time, &t) == NULL || 2367fa9e4066Sahrens strftime(propbuf, proplen, "%a %b %e %k:%M %Y", 2368fa9e4066Sahrens &t) == 0) 2369a2eea2e1Sahrens (void) snprintf(propbuf, proplen, "%llu", val); 2370fa9e4066Sahrens } 2371fa9e4066Sahrens break; 2372fa9e4066Sahrens 2373fa9e4066Sahrens case ZFS_PROP_MOUNTPOINT: 2374fa9e4066Sahrens /* 2375fa9e4066Sahrens * Getting the precise mountpoint can be tricky. 2376fa9e4066Sahrens * 2377fa9e4066Sahrens * - for 'none' or 'legacy', return those values. 2378fa9e4066Sahrens * - for inherited mountpoints, we want to take everything 2379fa9e4066Sahrens * after our ancestor and append it to the inherited value. 2380fa9e4066Sahrens * 2381fa9e4066Sahrens * If the pool has an alternate root, we want to prepend that 2382fa9e4066Sahrens * root to any values we return. 2383fa9e4066Sahrens */ 238429ab75c9Srm160521 23857f7322feSeschrock str = getprop_string(zhp, prop, &source); 2386fa9e4066Sahrens 2387b21718f0Sgw25295 if (str[0] == '/') { 238829ab75c9Srm160521 char buf[MAXPATHLEN]; 238929ab75c9Srm160521 char *root = buf; 2390a79992aaSTom Erickson const char *relpath; 2391fa9e4066Sahrens 2392a79992aaSTom Erickson /* 2393a79992aaSTom Erickson * If we inherit the mountpoint, even from a dataset 2394a79992aaSTom Erickson * with a received value, the source will be the path of 2395a79992aaSTom Erickson * the dataset we inherit from. If source is 2396a79992aaSTom Erickson * ZPROP_SOURCE_VAL_RECVD, the received value is not 2397a79992aaSTom Erickson * inherited. 2398a79992aaSTom Erickson */ 2399a79992aaSTom Erickson if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) { 2400a79992aaSTom Erickson relpath = ""; 2401a79992aaSTom Erickson } else { 2402a79992aaSTom Erickson relpath = zhp->zfs_name + strlen(source); 2403fa9e4066Sahrens if (relpath[0] == '/') 2404fa9e4066Sahrens relpath++; 2405a79992aaSTom Erickson } 2406b21718f0Sgw25295 240729ab75c9Srm160521 if ((zpool_get_prop(zhp->zpool_hdl, 2408c58b3526SAdam Stevko ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL, 2409c58b3526SAdam Stevko B_FALSE)) || (strcmp(root, "-") == 0)) 241029ab75c9Srm160521 root[0] = '\0'; 2411b21718f0Sgw25295 /* 2412b21718f0Sgw25295 * Special case an alternate root of '/'. This will 2413b21718f0Sgw25295 * avoid having multiple leading slashes in the 2414b21718f0Sgw25295 * mountpoint path. 2415b21718f0Sgw25295 */ 2416b21718f0Sgw25295 if (strcmp(root, "/") == 0) 2417b21718f0Sgw25295 root++; 2418b21718f0Sgw25295 2419b21718f0Sgw25295 /* 2420b21718f0Sgw25295 * If the mountpoint is '/' then skip over this 2421b21718f0Sgw25295 * if we are obtaining either an alternate root or 2422b21718f0Sgw25295 * an inherited mountpoint. 2423b21718f0Sgw25295 */ 2424b21718f0Sgw25295 if (str[1] == '\0' && (root[0] != '\0' || 2425b21718f0Sgw25295 relpath[0] != '\0')) 24267f7322feSeschrock str++; 2427fa9e4066Sahrens 2428fa9e4066Sahrens if (relpath[0] == '\0') 2429fa9e4066Sahrens (void) snprintf(propbuf, proplen, "%s%s", 24307f7322feSeschrock root, str); 2431fa9e4066Sahrens else 2432fa9e4066Sahrens (void) snprintf(propbuf, proplen, "%s%s%s%s", 24337f7322feSeschrock root, str, relpath[0] == '@' ? "" : "/", 2434fa9e4066Sahrens relpath); 2435fa9e4066Sahrens } else { 2436fa9e4066Sahrens /* 'legacy' or 'none' */ 24377f7322feSeschrock (void) strlcpy(propbuf, str, proplen); 2438fa9e4066Sahrens } 2439fa9e4066Sahrens 2440fa9e4066Sahrens break; 2441fa9e4066Sahrens 2442fa9e4066Sahrens case ZFS_PROP_ORIGIN: 24439c3fd121SMatthew Ahrens str = getprop_string(zhp, prop, &source); 24449c3fd121SMatthew Ahrens if (str == NULL) 2445fa9e4066Sahrens return (-1); 24469c3fd121SMatthew Ahrens (void) strlcpy(propbuf, str, proplen); 2447fa9e4066Sahrens break; 2448fa9e4066Sahrens 244919b94df9SMatthew Ahrens case ZFS_PROP_CLONES: 245019b94df9SMatthew Ahrens if (get_clones_string(zhp, propbuf, proplen) != 0) 245119b94df9SMatthew Ahrens return (-1); 245219b94df9SMatthew Ahrens break; 245319b94df9SMatthew Ahrens 2454fa9e4066Sahrens case ZFS_PROP_QUOTA: 2455a9799022Sck153898 case ZFS_PROP_REFQUOTA: 2456fa9e4066Sahrens case ZFS_PROP_RESERVATION: 2457a9799022Sck153898 case ZFS_PROP_REFRESERVATION: 2458a9799022Sck153898 245999653d4eSeschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 246099653d4eSeschrock return (-1); 2461fa9e4066Sahrens 2462fa9e4066Sahrens /* 2463fa9e4066Sahrens * If quota or reservation is 0, we translate this into 'none' 2464fa9e4066Sahrens * (unless literal is set), and indicate that it's the default 2465fa9e4066Sahrens * value. Otherwise, we print the number nicely and indicate 2466fa9e4066Sahrens * that its set locally. 2467fa9e4066Sahrens */ 2468fa9e4066Sahrens if (val == 0) { 2469fa9e4066Sahrens if (literal) 2470fa9e4066Sahrens (void) strlcpy(propbuf, "0", proplen); 2471fa9e4066Sahrens else 2472fa9e4066Sahrens (void) strlcpy(propbuf, "none", proplen); 2473fa9e4066Sahrens } else { 2474fa9e4066Sahrens if (literal) 24755ad82045Snd150628 (void) snprintf(propbuf, proplen, "%llu", 24765ad82045Snd150628 (u_longlong_t)val); 2477fa9e4066Sahrens else 2478fa9e4066Sahrens zfs_nicenum(val, propbuf, proplen); 2479fa9e4066Sahrens } 2480fa9e4066Sahrens break; 2481fa9e4066Sahrens 2482a2afb611SJerry Jelinek case ZFS_PROP_FILESYSTEM_LIMIT: 2483a2afb611SJerry Jelinek case ZFS_PROP_SNAPSHOT_LIMIT: 2484a2afb611SJerry Jelinek case ZFS_PROP_FILESYSTEM_COUNT: 2485a2afb611SJerry Jelinek case ZFS_PROP_SNAPSHOT_COUNT: 2486a2afb611SJerry Jelinek 2487a2afb611SJerry Jelinek if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 2488a2afb611SJerry Jelinek return (-1); 2489a2afb611SJerry Jelinek 2490a2afb611SJerry Jelinek /* 2491a2afb611SJerry Jelinek * If limit is UINT64_MAX, we translate this into 'none' (unless 2492a2afb611SJerry Jelinek * literal is set), and indicate that it's the default value. 2493a2afb611SJerry Jelinek * Otherwise, we print the number nicely and indicate that it's 2494a2afb611SJerry Jelinek * set locally. 2495a2afb611SJerry Jelinek */ 2496a2afb611SJerry Jelinek if (literal) { 2497a2afb611SJerry Jelinek (void) snprintf(propbuf, proplen, "%llu", 2498a2afb611SJerry Jelinek (u_longlong_t)val); 2499a2afb611SJerry Jelinek } else if (val == UINT64_MAX) { 2500a2afb611SJerry Jelinek (void) strlcpy(propbuf, "none", proplen); 2501a2afb611SJerry Jelinek } else { 2502a2afb611SJerry Jelinek zfs_nicenum(val, propbuf, proplen); 2503a2afb611SJerry Jelinek } 2504a2afb611SJerry Jelinek break; 2505a2afb611SJerry Jelinek 2506187d6ac0SMatt Ahrens case ZFS_PROP_REFRATIO: 2507fa9e4066Sahrens case ZFS_PROP_COMPRESSRATIO: 250899653d4eSeschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 250999653d4eSeschrock return (-1); 2510b24ab676SJeff Bonwick (void) snprintf(propbuf, proplen, "%llu.%02llux", 2511b24ab676SJeff Bonwick (u_longlong_t)(val / 100), 2512b24ab676SJeff Bonwick (u_longlong_t)(val % 100)); 2513fa9e4066Sahrens break; 2514fa9e4066Sahrens 2515fa9e4066Sahrens case ZFS_PROP_TYPE: 2516fa9e4066Sahrens switch (zhp->zfs_type) { 2517fa9e4066Sahrens case ZFS_TYPE_FILESYSTEM: 2518fa9e4066Sahrens str = "filesystem"; 2519fa9e4066Sahrens break; 2520fa9e4066Sahrens case ZFS_TYPE_VOLUME: 2521fa9e4066Sahrens str = "volume"; 2522fa9e4066Sahrens break; 2523fa9e4066Sahrens case ZFS_TYPE_SNAPSHOT: 2524fa9e4066Sahrens str = "snapshot"; 2525fa9e4066Sahrens break; 252678f17100SMatthew Ahrens case ZFS_TYPE_BOOKMARK: 252778f17100SMatthew Ahrens str = "bookmark"; 252878f17100SMatthew Ahrens break; 2529fa9e4066Sahrens default: 253099653d4eSeschrock abort(); 2531fa9e4066Sahrens } 2532fa9e4066Sahrens (void) snprintf(propbuf, proplen, "%s", str); 2533fa9e4066Sahrens break; 2534fa9e4066Sahrens 2535fa9e4066Sahrens case ZFS_PROP_MOUNTED: 2536fa9e4066Sahrens /* 2537fa9e4066Sahrens * The 'mounted' property is a pseudo-property that described 2538fa9e4066Sahrens * whether the filesystem is currently mounted. Even though 2539fa9e4066Sahrens * it's a boolean value, the typical values of "on" and "off" 2540fa9e4066Sahrens * don't make sense, so we translate to "yes" and "no". 2541fa9e4066Sahrens */ 254299653d4eSeschrock if (get_numeric_property(zhp, ZFS_PROP_MOUNTED, 254399653d4eSeschrock src, &source, &val) != 0) 254499653d4eSeschrock return (-1); 254599653d4eSeschrock if (val) 2546fa9e4066Sahrens (void) strlcpy(propbuf, "yes", proplen); 2547fa9e4066Sahrens else 2548fa9e4066Sahrens (void) strlcpy(propbuf, "no", proplen); 2549fa9e4066Sahrens break; 2550fa9e4066Sahrens 2551fa9e4066Sahrens case ZFS_PROP_NAME: 2552fa9e4066Sahrens /* 2553fa9e4066Sahrens * The 'name' property is a pseudo-property derived from the 2554fa9e4066Sahrens * dataset name. It is presented as a real property to simplify 2555fa9e4066Sahrens * consumers. 2556fa9e4066Sahrens */ 2557fa9e4066Sahrens (void) strlcpy(propbuf, zhp->zfs_name, proplen); 2558fa9e4066Sahrens break; 2559fa9e4066Sahrens 25604201a95eSRic Aleshire case ZFS_PROP_MLSLABEL: 25614201a95eSRic Aleshire { 25624201a95eSRic Aleshire m_label_t *new_sl = NULL; 25634201a95eSRic Aleshire char *ascii = NULL; /* human readable label */ 25644201a95eSRic Aleshire 25654201a95eSRic Aleshire (void) strlcpy(propbuf, 25664201a95eSRic Aleshire getprop_string(zhp, prop, &source), proplen); 25674201a95eSRic Aleshire 25684201a95eSRic Aleshire if (literal || (strcasecmp(propbuf, 25694201a95eSRic Aleshire ZFS_MLSLABEL_DEFAULT) == 0)) 25704201a95eSRic Aleshire break; 25714201a95eSRic Aleshire 25724201a95eSRic Aleshire /* 25734201a95eSRic Aleshire * Try to translate the internal hex string to 25744201a95eSRic Aleshire * human-readable output. If there are any 25754201a95eSRic Aleshire * problems just use the hex string. 25764201a95eSRic Aleshire */ 25774201a95eSRic Aleshire 25784201a95eSRic Aleshire if (str_to_label(propbuf, &new_sl, MAC_LABEL, 25794201a95eSRic Aleshire L_NO_CORRECTION, NULL) == -1) { 25804201a95eSRic Aleshire m_label_free(new_sl); 25814201a95eSRic Aleshire break; 25824201a95eSRic Aleshire } 25834201a95eSRic Aleshire 25844201a95eSRic Aleshire if (label_to_str(new_sl, &ascii, M_LABEL, 25854201a95eSRic Aleshire DEF_NAMES) != 0) { 25864201a95eSRic Aleshire if (ascii) 25874201a95eSRic Aleshire free(ascii); 25884201a95eSRic Aleshire m_label_free(new_sl); 25894201a95eSRic Aleshire break; 25904201a95eSRic Aleshire } 25914201a95eSRic Aleshire m_label_free(new_sl); 25924201a95eSRic Aleshire 25934201a95eSRic Aleshire (void) strlcpy(propbuf, ascii, proplen); 25944201a95eSRic Aleshire free(ascii); 25954201a95eSRic Aleshire } 25964201a95eSRic Aleshire break; 25974201a95eSRic Aleshire 2598f0f3ef5aSGarrett D'Amore case ZFS_PROP_GUID: 2599f0f3ef5aSGarrett D'Amore /* 2600f0f3ef5aSGarrett D'Amore * GUIDs are stored as numbers, but they are identifiers. 2601f0f3ef5aSGarrett D'Amore * We don't want them to be pretty printed, because pretty 2602f0f3ef5aSGarrett D'Amore * printing mangles the ID into a truncated and useless value. 2603f0f3ef5aSGarrett D'Amore */ 2604f0f3ef5aSGarrett D'Amore if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 2605f0f3ef5aSGarrett D'Amore return (-1); 2606f0f3ef5aSGarrett D'Amore (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val); 2607f0f3ef5aSGarrett D'Amore break; 2608f0f3ef5aSGarrett D'Amore 2609fa9e4066Sahrens default: 2610e7437265Sahrens switch (zfs_prop_get_type(prop)) { 261191ebeef5Sahrens case PROP_TYPE_NUMBER: 2612e7437265Sahrens if (get_numeric_property(zhp, prop, src, 2613e7437265Sahrens &source, &val) != 0) 2614e7437265Sahrens return (-1); 2615e7437265Sahrens if (literal) 2616e7437265Sahrens (void) snprintf(propbuf, proplen, "%llu", 2617e7437265Sahrens (u_longlong_t)val); 2618e7437265Sahrens else 2619e7437265Sahrens zfs_nicenum(val, propbuf, proplen); 2620e7437265Sahrens break; 2621e7437265Sahrens 262291ebeef5Sahrens case PROP_TYPE_STRING: 26239c3fd121SMatthew Ahrens str = getprop_string(zhp, prop, &source); 26249c3fd121SMatthew Ahrens if (str == NULL) 26259c3fd121SMatthew Ahrens return (-1); 26269c3fd121SMatthew Ahrens (void) strlcpy(propbuf, str, proplen); 2627e7437265Sahrens break; 2628e7437265Sahrens 262991ebeef5Sahrens case PROP_TYPE_INDEX: 2630fe192f76Sahrens if (get_numeric_property(zhp, prop, src, 2631fe192f76Sahrens &source, &val) != 0) 2632fe192f76Sahrens return (-1); 2633fe192f76Sahrens if (zfs_prop_index_to_string(prop, val, &strval) != 0) 2634e7437265Sahrens return (-1); 2635e7437265Sahrens (void) strlcpy(propbuf, strval, proplen); 2636e7437265Sahrens break; 2637e7437265Sahrens 2638e7437265Sahrens default: 263999653d4eSeschrock abort(); 2640fa9e4066Sahrens } 2641e7437265Sahrens } 2642fa9e4066Sahrens 2643fa9e4066Sahrens get_source(zhp, src, source, statbuf, statlen); 2644fa9e4066Sahrens 2645fa9e4066Sahrens return (0); 2646fa9e4066Sahrens } 2647fa9e4066Sahrens 2648fa9e4066Sahrens /* 2649fa9e4066Sahrens * Utility function to get the given numeric property. Does no validation that 2650fa9e4066Sahrens * the given property is the appropriate type; should only be used with 2651fa9e4066Sahrens * hard-coded property types. 2652fa9e4066Sahrens */ 2653fa9e4066Sahrens uint64_t 2654fa9e4066Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop) 2655fa9e4066Sahrens { 2656fa9e4066Sahrens char *source; 265799653d4eSeschrock uint64_t val; 2658fa9e4066Sahrens 26593cb34c60Sahrens (void) get_numeric_property(zhp, prop, NULL, &source, &val); 266099653d4eSeschrock 266199653d4eSeschrock return (val); 2662fa9e4066Sahrens } 2663fa9e4066Sahrens 26647b97dc1aSrm160521 int 26657b97dc1aSrm160521 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val) 26667b97dc1aSrm160521 { 26677b97dc1aSrm160521 char buf[64]; 26687b97dc1aSrm160521 266914843421SMatthew Ahrens (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val); 26707b97dc1aSrm160521 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf)); 26717b97dc1aSrm160521 } 26727b97dc1aSrm160521 2673fa9e4066Sahrens /* 2674fa9e4066Sahrens * Similar to zfs_prop_get(), but returns the value as an integer. 2675fa9e4066Sahrens */ 2676fa9e4066Sahrens int 2677fa9e4066Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value, 2678990b4856Slling zprop_source_t *src, char *statbuf, size_t statlen) 2679fa9e4066Sahrens { 2680fa9e4066Sahrens char *source; 2681fa9e4066Sahrens 2682fa9e4066Sahrens /* 2683fa9e4066Sahrens * Check to see if this property applies to our object 2684fa9e4066Sahrens */ 2685e45ce728Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) { 2686ece3d9b3Slling return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE, 268799653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot get property '%s'"), 268899653d4eSeschrock zfs_prop_to_name(prop))); 2689e45ce728Sahrens } 2690fa9e4066Sahrens 2691fa9e4066Sahrens if (src) 2692990b4856Slling *src = ZPROP_SRC_NONE; 2693fa9e4066Sahrens 269499653d4eSeschrock if (get_numeric_property(zhp, prop, src, &source, value) != 0) 269599653d4eSeschrock return (-1); 2696fa9e4066Sahrens 2697fa9e4066Sahrens get_source(zhp, src, source, statbuf, statlen); 2698fa9e4066Sahrens 2699fa9e4066Sahrens return (0); 2700fa9e4066Sahrens } 2701fa9e4066Sahrens 270214843421SMatthew Ahrens static int 270314843421SMatthew Ahrens idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser, 270414843421SMatthew Ahrens char **domainp, idmap_rid_t *ridp) 270514843421SMatthew Ahrens { 270614843421SMatthew Ahrens idmap_get_handle_t *get_hdl = NULL; 270714843421SMatthew Ahrens idmap_stat status; 270814843421SMatthew Ahrens int err = EINVAL; 270914843421SMatthew Ahrens 27101fdeec65Sjoyce mcintosh if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS) 271114843421SMatthew Ahrens goto out; 271214843421SMatthew Ahrens 271314843421SMatthew Ahrens if (isuser) { 271414843421SMatthew Ahrens err = idmap_get_sidbyuid(get_hdl, id, 271514843421SMatthew Ahrens IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status); 271614843421SMatthew Ahrens } else { 271714843421SMatthew Ahrens err = idmap_get_sidbygid(get_hdl, id, 271814843421SMatthew Ahrens IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status); 271914843421SMatthew Ahrens } 272014843421SMatthew Ahrens if (err == IDMAP_SUCCESS && 272114843421SMatthew Ahrens idmap_get_mappings(get_hdl) == IDMAP_SUCCESS && 272214843421SMatthew Ahrens status == IDMAP_SUCCESS) 272314843421SMatthew Ahrens err = 0; 272414843421SMatthew Ahrens else 272514843421SMatthew Ahrens err = EINVAL; 272614843421SMatthew Ahrens out: 272714843421SMatthew Ahrens if (get_hdl) 272814843421SMatthew Ahrens idmap_get_destroy(get_hdl); 272914843421SMatthew Ahrens return (err); 273014843421SMatthew Ahrens } 273114843421SMatthew Ahrens 273214843421SMatthew Ahrens /* 273314843421SMatthew Ahrens * convert the propname into parameters needed by kernel 273414843421SMatthew Ahrens * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829 273514843421SMatthew Ahrens * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789 273614843421SMatthew Ahrens */ 273714843421SMatthew Ahrens static int 273814843421SMatthew Ahrens userquota_propname_decode(const char *propname, boolean_t zoned, 273914843421SMatthew Ahrens zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp) 274014843421SMatthew Ahrens { 274114843421SMatthew Ahrens zfs_userquota_prop_t type; 274214843421SMatthew Ahrens char *cp, *end; 27433b12c289SMatthew Ahrens char *numericsid = NULL; 274414843421SMatthew Ahrens boolean_t isuser; 274514843421SMatthew Ahrens 274614843421SMatthew Ahrens domain[0] = '\0'; 27471ed6b69aSGordon Ross *ridp = 0; 274814843421SMatthew Ahrens /* Figure out the property type ({user|group}{quota|space}) */ 274914843421SMatthew Ahrens for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) { 275014843421SMatthew Ahrens if (strncmp(propname, zfs_userquota_prop_prefixes[type], 275114843421SMatthew Ahrens strlen(zfs_userquota_prop_prefixes[type])) == 0) 275214843421SMatthew Ahrens break; 275314843421SMatthew Ahrens } 275414843421SMatthew Ahrens if (type == ZFS_NUM_USERQUOTA_PROPS) 275514843421SMatthew Ahrens return (EINVAL); 275614843421SMatthew Ahrens *typep = type; 275714843421SMatthew Ahrens 275814843421SMatthew Ahrens isuser = (type == ZFS_PROP_USERQUOTA || 275914843421SMatthew Ahrens type == ZFS_PROP_USERUSED); 276014843421SMatthew Ahrens 276114843421SMatthew Ahrens cp = strchr(propname, '@') + 1; 276214843421SMatthew Ahrens 276314843421SMatthew Ahrens if (strchr(cp, '@')) { 276414843421SMatthew Ahrens /* 276514843421SMatthew Ahrens * It's a SID name (eg "user@domain") that needs to be 27663b12c289SMatthew Ahrens * turned into S-1-domainID-RID. 276714843421SMatthew Ahrens */ 27681ed6b69aSGordon Ross int flag = 0; 27691ed6b69aSGordon Ross idmap_stat stat, map_stat; 27701ed6b69aSGordon Ross uid_t pid; 27711ed6b69aSGordon Ross idmap_rid_t rid; 27721ed6b69aSGordon Ross idmap_get_handle_t *gh = NULL; 27731ed6b69aSGordon Ross 27741ed6b69aSGordon Ross stat = idmap_get_create(&gh); 27751ed6b69aSGordon Ross if (stat != IDMAP_SUCCESS) { 27761ed6b69aSGordon Ross idmap_get_destroy(gh); 27771ed6b69aSGordon Ross return (ENOMEM); 27781ed6b69aSGordon Ross } 277914843421SMatthew Ahrens if (zoned && getzoneid() == GLOBAL_ZONEID) 278014843421SMatthew Ahrens return (ENOENT); 27813b12c289SMatthew Ahrens if (isuser) { 27821ed6b69aSGordon Ross stat = idmap_getuidbywinname(cp, NULL, flag, &pid); 27831ed6b69aSGordon Ross if (stat < 0) 27841ed6b69aSGordon Ross return (ENOENT); 27851ed6b69aSGordon Ross stat = idmap_get_sidbyuid(gh, pid, flag, &numericsid, 27861ed6b69aSGordon Ross &rid, &map_stat); 27873b12c289SMatthew Ahrens } else { 27881ed6b69aSGordon Ross stat = idmap_getgidbywinname(cp, NULL, flag, &pid); 27891ed6b69aSGordon Ross if (stat < 0) 27901ed6b69aSGordon Ross return (ENOENT); 27911ed6b69aSGordon Ross stat = idmap_get_sidbygid(gh, pid, flag, &numericsid, 27921ed6b69aSGordon Ross &rid, &map_stat); 27933b12c289SMatthew Ahrens } 27941ed6b69aSGordon Ross if (stat < 0) { 27951ed6b69aSGordon Ross idmap_get_destroy(gh); 27961ed6b69aSGordon Ross return (ENOENT); 27971ed6b69aSGordon Ross } 27981ed6b69aSGordon Ross stat = idmap_get_mappings(gh); 27991ed6b69aSGordon Ross idmap_get_destroy(gh); 28001ed6b69aSGordon Ross 28011ed6b69aSGordon Ross if (stat < 0) { 280214843421SMatthew Ahrens return (ENOENT); 28033b12c289SMatthew Ahrens } 28043b12c289SMatthew Ahrens if (numericsid == NULL) 280514843421SMatthew Ahrens return (ENOENT); 28063b12c289SMatthew Ahrens cp = numericsid; 28071ed6b69aSGordon Ross *ridp = rid; 28083b12c289SMatthew Ahrens /* will be further decoded below */ 28093b12c289SMatthew Ahrens } 28103b12c289SMatthew Ahrens 28113b12c289SMatthew Ahrens if (strncmp(cp, "S-1-", 4) == 0) { 281214843421SMatthew Ahrens /* It's a numeric SID (eg "S-1-234-567-89") */ 28133b12c289SMatthew Ahrens (void) strlcpy(domain, cp, domainlen); 28141ed6b69aSGordon Ross errno = 0; 28151ed6b69aSGordon Ross if (*ridp == 0) { 281614843421SMatthew Ahrens cp = strrchr(domain, '-'); 281714843421SMatthew Ahrens *cp = '\0'; 281814843421SMatthew Ahrens cp++; 281914843421SMatthew Ahrens *ridp = strtoull(cp, &end, 10); 28201ed6b69aSGordon Ross } else { 28211ed6b69aSGordon Ross end = ""; 28221ed6b69aSGordon Ross } 28233b12c289SMatthew Ahrens if (numericsid) { 28243b12c289SMatthew Ahrens free(numericsid); 28253b12c289SMatthew Ahrens numericsid = NULL; 28263b12c289SMatthew Ahrens } 2827777badbaSMatthew Ahrens if (errno != 0 || *end != '\0') 282814843421SMatthew Ahrens return (EINVAL); 282914843421SMatthew Ahrens } else if (!isdigit(*cp)) { 283014843421SMatthew Ahrens /* 283114843421SMatthew Ahrens * It's a user/group name (eg "user") that needs to be 283214843421SMatthew Ahrens * turned into a uid/gid 283314843421SMatthew Ahrens */ 283414843421SMatthew Ahrens if (zoned && getzoneid() == GLOBAL_ZONEID) 283514843421SMatthew Ahrens return (ENOENT); 283614843421SMatthew Ahrens if (isuser) { 283714843421SMatthew Ahrens struct passwd *pw; 283814843421SMatthew Ahrens pw = getpwnam(cp); 283914843421SMatthew Ahrens if (pw == NULL) 284014843421SMatthew Ahrens return (ENOENT); 284114843421SMatthew Ahrens *ridp = pw->pw_uid; 284214843421SMatthew Ahrens } else { 284314843421SMatthew Ahrens struct group *gr; 284414843421SMatthew Ahrens gr = getgrnam(cp); 284514843421SMatthew Ahrens if (gr == NULL) 284614843421SMatthew Ahrens return (ENOENT); 284714843421SMatthew Ahrens *ridp = gr->gr_gid; 284814843421SMatthew Ahrens } 284914843421SMatthew Ahrens } else { 285014843421SMatthew Ahrens /* It's a user/group ID (eg "12345"). */ 285114843421SMatthew Ahrens uid_t id = strtoul(cp, &end, 10); 285214843421SMatthew Ahrens idmap_rid_t rid; 285314843421SMatthew Ahrens char *mapdomain; 285414843421SMatthew Ahrens 285514843421SMatthew Ahrens if (*end != '\0') 285614843421SMatthew Ahrens return (EINVAL); 285714843421SMatthew Ahrens if (id > MAXUID) { 285814843421SMatthew Ahrens /* It's an ephemeral ID. */ 285914843421SMatthew Ahrens if (idmap_id_to_numeric_domain_rid(id, isuser, 286014843421SMatthew Ahrens &mapdomain, &rid) != 0) 286114843421SMatthew Ahrens return (ENOENT); 28623b12c289SMatthew Ahrens (void) strlcpy(domain, mapdomain, domainlen); 286314843421SMatthew Ahrens *ridp = rid; 286414843421SMatthew Ahrens } else { 286514843421SMatthew Ahrens *ridp = id; 286614843421SMatthew Ahrens } 286714843421SMatthew Ahrens } 286814843421SMatthew Ahrens 28693b12c289SMatthew Ahrens ASSERT3P(numericsid, ==, NULL); 287014843421SMatthew Ahrens return (0); 287114843421SMatthew Ahrens } 287214843421SMatthew Ahrens 2873edea4b55SLin Ling static int 2874edea4b55SLin Ling zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname, 2875edea4b55SLin Ling uint64_t *propvalue, zfs_userquota_prop_t *typep) 287614843421SMatthew Ahrens { 287714843421SMatthew Ahrens int err; 287814843421SMatthew Ahrens zfs_cmd_t zc = { 0 }; 287914843421SMatthew Ahrens 288019b94df9SMatthew Ahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 288114843421SMatthew Ahrens 288214843421SMatthew Ahrens err = userquota_propname_decode(propname, 288314843421SMatthew Ahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED), 2884edea4b55SLin Ling typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid); 2885edea4b55SLin Ling zc.zc_objset_type = *typep; 288614843421SMatthew Ahrens if (err) 288714843421SMatthew Ahrens return (err); 288814843421SMatthew Ahrens 288914843421SMatthew Ahrens err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc); 289014843421SMatthew Ahrens if (err) 289114843421SMatthew Ahrens return (err); 289214843421SMatthew Ahrens 2893edea4b55SLin Ling *propvalue = zc.zc_cookie; 2894edea4b55SLin Ling return (0); 2895edea4b55SLin Ling } 2896edea4b55SLin Ling 2897edea4b55SLin Ling int 2898edea4b55SLin Ling zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname, 2899edea4b55SLin Ling uint64_t *propvalue) 2900edea4b55SLin Ling { 2901edea4b55SLin Ling zfs_userquota_prop_t type; 2902edea4b55SLin Ling 2903edea4b55SLin Ling return (zfs_prop_get_userquota_common(zhp, propname, propvalue, 2904edea4b55SLin Ling &type)); 2905edea4b55SLin Ling } 2906edea4b55SLin Ling 2907edea4b55SLin Ling int 2908edea4b55SLin Ling zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname, 2909edea4b55SLin Ling char *propbuf, int proplen, boolean_t literal) 2910edea4b55SLin Ling { 2911edea4b55SLin Ling int err; 2912edea4b55SLin Ling uint64_t propvalue; 2913edea4b55SLin Ling zfs_userquota_prop_t type; 2914edea4b55SLin Ling 2915edea4b55SLin Ling err = zfs_prop_get_userquota_common(zhp, propname, &propvalue, 2916edea4b55SLin Ling &type); 2917edea4b55SLin Ling 2918edea4b55SLin Ling if (err) 2919edea4b55SLin Ling return (err); 2920edea4b55SLin Ling 292114843421SMatthew Ahrens if (literal) { 2922edea4b55SLin Ling (void) snprintf(propbuf, proplen, "%llu", propvalue); 2923edea4b55SLin Ling } else if (propvalue == 0 && 292414843421SMatthew Ahrens (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) { 292514843421SMatthew Ahrens (void) strlcpy(propbuf, "none", proplen); 292614843421SMatthew Ahrens } else { 2927edea4b55SLin Ling zfs_nicenum(propvalue, propbuf, proplen); 292814843421SMatthew Ahrens } 292914843421SMatthew Ahrens return (0); 293014843421SMatthew Ahrens } 293114843421SMatthew Ahrens 293219b94df9SMatthew Ahrens int 293319b94df9SMatthew Ahrens zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname, 293419b94df9SMatthew Ahrens uint64_t *propvalue) 293519b94df9SMatthew Ahrens { 293619b94df9SMatthew Ahrens int err; 293719b94df9SMatthew Ahrens zfs_cmd_t zc = { 0 }; 293819b94df9SMatthew Ahrens const char *snapname; 293919b94df9SMatthew Ahrens 294019b94df9SMatthew Ahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 294119b94df9SMatthew Ahrens 294219b94df9SMatthew Ahrens snapname = strchr(propname, '@') + 1; 294319b94df9SMatthew Ahrens if (strchr(snapname, '@')) { 294419b94df9SMatthew Ahrens (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value)); 294519b94df9SMatthew Ahrens } else { 294619b94df9SMatthew Ahrens /* snapname is the short name, append it to zhp's fsname */ 294719b94df9SMatthew Ahrens char *cp; 294819b94df9SMatthew Ahrens 294919b94df9SMatthew Ahrens (void) strlcpy(zc.zc_value, zhp->zfs_name, 295019b94df9SMatthew Ahrens sizeof (zc.zc_value)); 295119b94df9SMatthew Ahrens cp = strchr(zc.zc_value, '@'); 295219b94df9SMatthew Ahrens if (cp != NULL) 295319b94df9SMatthew Ahrens *cp = '\0'; 295419b94df9SMatthew Ahrens (void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value)); 295519b94df9SMatthew Ahrens (void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value)); 295619b94df9SMatthew Ahrens } 295719b94df9SMatthew Ahrens 295819b94df9SMatthew Ahrens err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc); 295919b94df9SMatthew Ahrens if (err) 296019b94df9SMatthew Ahrens return (err); 296119b94df9SMatthew Ahrens 296219b94df9SMatthew Ahrens *propvalue = zc.zc_cookie; 296319b94df9SMatthew Ahrens return (0); 296419b94df9SMatthew Ahrens } 296519b94df9SMatthew Ahrens 296619b94df9SMatthew Ahrens int 296719b94df9SMatthew Ahrens zfs_prop_get_written(zfs_handle_t *zhp, const char *propname, 296819b94df9SMatthew Ahrens char *propbuf, int proplen, boolean_t literal) 296919b94df9SMatthew Ahrens { 297019b94df9SMatthew Ahrens int err; 297119b94df9SMatthew Ahrens uint64_t propvalue; 297219b94df9SMatthew Ahrens 297319b94df9SMatthew Ahrens err = zfs_prop_get_written_int(zhp, propname, &propvalue); 297419b94df9SMatthew Ahrens 297519b94df9SMatthew Ahrens if (err) 297619b94df9SMatthew Ahrens return (err); 297719b94df9SMatthew Ahrens 297819b94df9SMatthew Ahrens if (literal) { 297919b94df9SMatthew Ahrens (void) snprintf(propbuf, proplen, "%llu", propvalue); 298019b94df9SMatthew Ahrens } else { 298119b94df9SMatthew Ahrens zfs_nicenum(propvalue, propbuf, proplen); 298219b94df9SMatthew Ahrens } 298319b94df9SMatthew Ahrens return (0); 298419b94df9SMatthew Ahrens } 298519b94df9SMatthew Ahrens 2986fa9e4066Sahrens /* 2987fa9e4066Sahrens * Returns the name of the given zfs handle. 2988fa9e4066Sahrens */ 2989fa9e4066Sahrens const char * 2990fa9e4066Sahrens zfs_get_name(const zfs_handle_t *zhp) 2991fa9e4066Sahrens { 2992fa9e4066Sahrens return (zhp->zfs_name); 2993fa9e4066Sahrens } 2994fa9e4066Sahrens 2995fa9e4066Sahrens /* 29968808ac5dSYuri Pankov * Returns the name of the parent pool for the given zfs handle. 29978808ac5dSYuri Pankov */ 29988808ac5dSYuri Pankov const char * 29998808ac5dSYuri Pankov zfs_get_pool_name(const zfs_handle_t *zhp) 30008808ac5dSYuri Pankov { 30018808ac5dSYuri Pankov return (zhp->zpool_hdl->zpool_name); 30028808ac5dSYuri Pankov } 30038808ac5dSYuri Pankov 30048808ac5dSYuri Pankov /* 3005fa9e4066Sahrens * Returns the type of the given zfs handle. 3006fa9e4066Sahrens */ 3007fa9e4066Sahrens zfs_type_t 3008fa9e4066Sahrens zfs_get_type(const zfs_handle_t *zhp) 3009fa9e4066Sahrens { 3010fa9e4066Sahrens return (zhp->zfs_type); 3011fa9e4066Sahrens } 3012fa9e4066Sahrens 30137f7322feSeschrock /* 3014d41c4376SMark J Musante * Is one dataset name a child dataset of another? 3015d41c4376SMark J Musante * 3016d41c4376SMark J Musante * Needs to handle these cases: 3017d41c4376SMark J Musante * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo" 3018d41c4376SMark J Musante * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar" 3019d41c4376SMark J Musante * Descendant? No. No. No. Yes. 3020d41c4376SMark J Musante */ 3021d41c4376SMark J Musante static boolean_t 3022d41c4376SMark J Musante is_descendant(const char *ds1, const char *ds2) 3023d41c4376SMark J Musante { 3024d41c4376SMark J Musante size_t d1len = strlen(ds1); 3025d41c4376SMark J Musante 3026d41c4376SMark J Musante /* ds2 can't be a descendant if it's smaller */ 3027d41c4376SMark J Musante if (strlen(ds2) < d1len) 3028d41c4376SMark J Musante return (B_FALSE); 3029d41c4376SMark J Musante 3030d41c4376SMark J Musante /* otherwise, compare strings and verify that there's a '/' char */ 3031d41c4376SMark J Musante return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0)); 3032d41c4376SMark J Musante } 3033d41c4376SMark J Musante 3034d41c4376SMark J Musante /* 3035fa9e4066Sahrens * Given a complete name, return just the portion that refers to the parent. 303619b94df9SMatthew Ahrens * Will return -1 if there is no parent (path is just the name of the 303719b94df9SMatthew Ahrens * pool). 3038fa9e4066Sahrens */ 3039fa9e4066Sahrens static int 3040fa9e4066Sahrens parent_name(const char *path, char *buf, size_t buflen) 3041fa9e4066Sahrens { 304219b94df9SMatthew Ahrens char *slashp; 3043fa9e4066Sahrens 304419b94df9SMatthew Ahrens (void) strlcpy(buf, path, buflen); 304519b94df9SMatthew Ahrens 304619b94df9SMatthew Ahrens if ((slashp = strrchr(buf, '/')) == NULL) 3047fa9e4066Sahrens return (-1); 304819b94df9SMatthew Ahrens *slashp = '\0'; 3049fa9e4066Sahrens 3050fa9e4066Sahrens return (0); 3051fa9e4066Sahrens } 3052fa9e4066Sahrens 3053fa9e4066Sahrens /* 30547f1f55eaSvb160487 * If accept_ancestor is false, then check to make sure that the given path has 30557f1f55eaSvb160487 * a parent, and that it exists. If accept_ancestor is true, then find the 30567f1f55eaSvb160487 * closest existing ancestor for the given path. In prefixlen return the 30577f1f55eaSvb160487 * length of already existing prefix of the given path. We also fetch the 30587f1f55eaSvb160487 * 'zoned' property, which is used to validate property settings when creating 30597f1f55eaSvb160487 * new datasets. 3060fa9e4066Sahrens */ 3061fa9e4066Sahrens static int 30627f1f55eaSvb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned, 30637f1f55eaSvb160487 boolean_t accept_ancestor, int *prefixlen) 3064fa9e4066Sahrens { 3065fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 30669adfa60dSMatthew Ahrens char parent[ZFS_MAX_DATASET_NAME_LEN]; 3067fa9e4066Sahrens char *slash; 30687f7322feSeschrock zfs_handle_t *zhp; 306999653d4eSeschrock char errbuf[1024]; 3070d41c4376SMark J Musante uint64_t is_zoned; 307199653d4eSeschrock 3072deb8317bSMark J Musante (void) snprintf(errbuf, sizeof (errbuf), 3073deb8317bSMark J Musante dgettext(TEXT_DOMAIN, "cannot create '%s'"), path); 3074fa9e4066Sahrens 3075fa9e4066Sahrens /* get parent, and check to see if this is just a pool */ 3076fa9e4066Sahrens if (parent_name(path, parent, sizeof (parent)) != 0) { 307799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 307899653d4eSeschrock "missing dataset name")); 307999653d4eSeschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3080fa9e4066Sahrens } 3081fa9e4066Sahrens 3082fa9e4066Sahrens /* check to see if the pool exists */ 3083fa9e4066Sahrens if ((slash = strchr(parent, '/')) == NULL) 3084fa9e4066Sahrens slash = parent + strlen(parent); 30858ac09fceSRichard Lowe (void) strncpy(zc.zc_name, parent, slash - parent); 3086fa9e4066Sahrens zc.zc_name[slash - parent] = '\0'; 308799653d4eSeschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 && 3088fa9e4066Sahrens errno == ENOENT) { 308999653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 309099653d4eSeschrock "no such pool '%s'"), zc.zc_name); 309199653d4eSeschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 3092fa9e4066Sahrens } 3093fa9e4066Sahrens 3094fa9e4066Sahrens /* check to see if the parent dataset exists */ 30957f1f55eaSvb160487 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) { 30967f1f55eaSvb160487 if (errno == ENOENT && accept_ancestor) { 30977f1f55eaSvb160487 /* 30987f1f55eaSvb160487 * Go deeper to find an ancestor, give up on top level. 30997f1f55eaSvb160487 */ 31007f1f55eaSvb160487 if (parent_name(parent, parent, sizeof (parent)) != 0) { 31017f1f55eaSvb160487 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 31027f1f55eaSvb160487 "no such pool '%s'"), zc.zc_name); 31037f1f55eaSvb160487 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 31047f1f55eaSvb160487 } 31057f1f55eaSvb160487 } else if (errno == ENOENT) { 310699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 310799653d4eSeschrock "parent does not exist")); 310899653d4eSeschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 31097f1f55eaSvb160487 } else 311099653d4eSeschrock return (zfs_standard_error(hdl, errno, errbuf)); 3111fa9e4066Sahrens } 3112fa9e4066Sahrens 3113d41c4376SMark J Musante is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); 3114d41c4376SMark J Musante if (zoned != NULL) 3115d41c4376SMark J Musante *zoned = is_zoned; 3116d41c4376SMark J Musante 3117fa9e4066Sahrens /* we are in a non-global zone, but parent is in the global zone */ 3118d41c4376SMark J Musante if (getzoneid() != GLOBAL_ZONEID && !is_zoned) { 311999653d4eSeschrock (void) zfs_standard_error(hdl, EPERM, errbuf); 31207f7322feSeschrock zfs_close(zhp); 3121fa9e4066Sahrens return (-1); 3122fa9e4066Sahrens } 3123fa9e4066Sahrens 3124fa9e4066Sahrens /* make sure parent is a filesystem */ 31257f7322feSeschrock if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) { 312699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 312799653d4eSeschrock "parent is not a filesystem")); 312899653d4eSeschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 31297f7322feSeschrock zfs_close(zhp); 3130fa9e4066Sahrens return (-1); 3131fa9e4066Sahrens } 3132fa9e4066Sahrens 31337f7322feSeschrock zfs_close(zhp); 31347f1f55eaSvb160487 if (prefixlen != NULL) 31357f1f55eaSvb160487 *prefixlen = strlen(parent); 31367f1f55eaSvb160487 return (0); 31377f1f55eaSvb160487 } 31387f1f55eaSvb160487 31397f1f55eaSvb160487 /* 31407f1f55eaSvb160487 * Finds whether the dataset of the given type(s) exists. 31417f1f55eaSvb160487 */ 31427f1f55eaSvb160487 boolean_t 31437f1f55eaSvb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types) 31447f1f55eaSvb160487 { 31457f1f55eaSvb160487 zfs_handle_t *zhp; 31467f1f55eaSvb160487 3147f18faf3fSek110237 if (!zfs_validate_name(hdl, path, types, B_FALSE)) 31487f1f55eaSvb160487 return (B_FALSE); 31497f1f55eaSvb160487 31507f1f55eaSvb160487 /* 31517f1f55eaSvb160487 * Try to get stats for the dataset, which will tell us if it exists. 31527f1f55eaSvb160487 */ 31537f1f55eaSvb160487 if ((zhp = make_dataset_handle(hdl, path)) != NULL) { 31547f1f55eaSvb160487 int ds_type = zhp->zfs_type; 31557f1f55eaSvb160487 31567f1f55eaSvb160487 zfs_close(zhp); 31577f1f55eaSvb160487 if (types & ds_type) 31587f1f55eaSvb160487 return (B_TRUE); 31597f1f55eaSvb160487 } 31607f1f55eaSvb160487 return (B_FALSE); 31617f1f55eaSvb160487 } 31627f1f55eaSvb160487 31637f1f55eaSvb160487 /* 31643cb34c60Sahrens * Given a path to 'target', create all the ancestors between 31653cb34c60Sahrens * the prefixlen portion of the path, and the target itself. 31663cb34c60Sahrens * Fail if the initial prefixlen-ancestor does not already exist. 31673cb34c60Sahrens */ 31683cb34c60Sahrens int 31693cb34c60Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen) 31703cb34c60Sahrens { 31713cb34c60Sahrens zfs_handle_t *h; 31723cb34c60Sahrens char *cp; 31733cb34c60Sahrens const char *opname; 31743cb34c60Sahrens 31753cb34c60Sahrens /* make sure prefix exists */ 31763cb34c60Sahrens cp = target + prefixlen; 31773cb34c60Sahrens if (*cp != '/') { 31783cb34c60Sahrens assert(strchr(cp, '/') == NULL); 31793cb34c60Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 31803cb34c60Sahrens } else { 31813cb34c60Sahrens *cp = '\0'; 31823cb34c60Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 31833cb34c60Sahrens *cp = '/'; 31843cb34c60Sahrens } 31853cb34c60Sahrens if (h == NULL) 31863cb34c60Sahrens return (-1); 31873cb34c60Sahrens zfs_close(h); 31883cb34c60Sahrens 31893cb34c60Sahrens /* 31903cb34c60Sahrens * Attempt to create, mount, and share any ancestor filesystems, 31913cb34c60Sahrens * up to the prefixlen-long one. 31923cb34c60Sahrens */ 31933cb34c60Sahrens for (cp = target + prefixlen + 1; 319488f61deeSIgor Kozhukhov (cp = strchr(cp, '/')) != NULL; *cp = '/', cp++) { 31953cb34c60Sahrens 31963cb34c60Sahrens *cp = '\0'; 31973cb34c60Sahrens 31983cb34c60Sahrens h = make_dataset_handle(hdl, target); 31993cb34c60Sahrens if (h) { 32003cb34c60Sahrens /* it already exists, nothing to do here */ 32013cb34c60Sahrens zfs_close(h); 32023cb34c60Sahrens continue; 32033cb34c60Sahrens } 32043cb34c60Sahrens 32053cb34c60Sahrens if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM, 32063cb34c60Sahrens NULL) != 0) { 32073cb34c60Sahrens opname = dgettext(TEXT_DOMAIN, "create"); 32083cb34c60Sahrens goto ancestorerr; 32093cb34c60Sahrens } 32103cb34c60Sahrens 32113cb34c60Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 32123cb34c60Sahrens if (h == NULL) { 32133cb34c60Sahrens opname = dgettext(TEXT_DOMAIN, "open"); 32143cb34c60Sahrens goto ancestorerr; 32153cb34c60Sahrens } 32163cb34c60Sahrens 32173cb34c60Sahrens if (zfs_mount(h, NULL, 0) != 0) { 32183cb34c60Sahrens opname = dgettext(TEXT_DOMAIN, "mount"); 32193cb34c60Sahrens goto ancestorerr; 32203cb34c60Sahrens } 32213cb34c60Sahrens 32223cb34c60Sahrens if (zfs_share(h) != 0) { 32233cb34c60Sahrens opname = dgettext(TEXT_DOMAIN, "share"); 32243cb34c60Sahrens goto ancestorerr; 32253cb34c60Sahrens } 32263cb34c60Sahrens 32273cb34c60Sahrens zfs_close(h); 32283cb34c60Sahrens } 32293cb34c60Sahrens 32303cb34c60Sahrens return (0); 32313cb34c60Sahrens 32323cb34c60Sahrens ancestorerr: 32333cb34c60Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 32343cb34c60Sahrens "failed to %s ancestor '%s'"), opname, target); 32353cb34c60Sahrens return (-1); 32363cb34c60Sahrens } 32373cb34c60Sahrens 32383cb34c60Sahrens /* 32397f1f55eaSvb160487 * Creates non-existing ancestors of the given path. 32407f1f55eaSvb160487 */ 32417f1f55eaSvb160487 int 32427f1f55eaSvb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path) 32437f1f55eaSvb160487 { 32447f1f55eaSvb160487 int prefix; 32457f1f55eaSvb160487 char *path_copy; 3246f83b46baSPaul Dagnelie int rc = 0; 32477f1f55eaSvb160487 3248d41c4376SMark J Musante if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0) 32497f1f55eaSvb160487 return (-1); 32507f1f55eaSvb160487 32517f1f55eaSvb160487 if ((path_copy = strdup(path)) != NULL) { 32527f1f55eaSvb160487 rc = create_parents(hdl, path_copy, prefix); 32537f1f55eaSvb160487 free(path_copy); 32547f1f55eaSvb160487 } 32557f1f55eaSvb160487 if (path_copy == NULL || rc != 0) 32567f1f55eaSvb160487 return (-1); 32577f1f55eaSvb160487 3258fa9e4066Sahrens return (0); 3259fa9e4066Sahrens } 3260fa9e4066Sahrens 3261fa9e4066Sahrens /* 3262e9dbad6fSeschrock * Create a new filesystem or volume. 3263fa9e4066Sahrens */ 3264fa9e4066Sahrens int 326599653d4eSeschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type, 3266e9dbad6fSeschrock nvlist_t *props) 3267fa9e4066Sahrens { 3268fa9e4066Sahrens int ret; 3269fa9e4066Sahrens uint64_t size = 0; 3270fa9e4066Sahrens uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 327199653d4eSeschrock char errbuf[1024]; 3272e9dbad6fSeschrock uint64_t zoned; 327326455f9eSAndriy Gapon enum lzc_dataset_type ost; 3274*690031d3Sloli10K zpool_handle_t *zpool_handle; 327599653d4eSeschrock 327699653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 327799653d4eSeschrock "cannot create '%s'"), path); 3278fa9e4066Sahrens 3279fa9e4066Sahrens /* validate the path, taking care to note the extended error message */ 3280f18faf3fSek110237 if (!zfs_validate_name(hdl, path, type, B_TRUE)) 328199653d4eSeschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3282fa9e4066Sahrens 3283fa9e4066Sahrens /* validate parents exist */ 32847f1f55eaSvb160487 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0) 3285fa9e4066Sahrens return (-1); 3286fa9e4066Sahrens 3287fa9e4066Sahrens /* 3288fa9e4066Sahrens * The failure modes when creating a dataset of a different type over 3289fa9e4066Sahrens * one that already exists is a little strange. In particular, if you 3290fa9e4066Sahrens * try to create a dataset on top of an existing dataset, the ioctl() 3291fa9e4066Sahrens * will return ENOENT, not EEXIST. To prevent this from happening, we 3292fa9e4066Sahrens * first try to see if the dataset exists. 3293fa9e4066Sahrens */ 32944445fffbSMatthew Ahrens if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) { 329599653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 329699653d4eSeschrock "dataset already exists")); 329799653d4eSeschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3298fa9e4066Sahrens } 3299fa9e4066Sahrens 3300fa9e4066Sahrens if (type == ZFS_TYPE_VOLUME) 330126455f9eSAndriy Gapon ost = LZC_DATSET_TYPE_ZVOL; 3302fa9e4066Sahrens else 330326455f9eSAndriy Gapon ost = LZC_DATSET_TYPE_ZFS; 3304fa9e4066Sahrens 3305e9316f76SJoe Stein /* open zpool handle for prop validation */ 33069adfa60dSMatthew Ahrens char pool_path[ZFS_MAX_DATASET_NAME_LEN]; 3307e9316f76SJoe Stein (void) strlcpy(pool_path, path, sizeof (pool_path)); 3308e9316f76SJoe Stein 3309e9316f76SJoe Stein /* truncate pool_path at first slash */ 3310e9316f76SJoe Stein char *p = strchr(pool_path, '/'); 3311e9316f76SJoe Stein if (p != NULL) 3312e9316f76SJoe Stein *p = '\0'; 3313e9316f76SJoe Stein 3314*690031d3Sloli10K if ((zpool_handle = zpool_open(hdl, pool_path)) == NULL) 3315*690031d3Sloli10K return (-1); 3316e9316f76SJoe Stein 33170a48a24eStimh if (props && (props = zfs_valid_proplist(hdl, type, props, 3318e9316f76SJoe Stein zoned, NULL, zpool_handle, errbuf)) == 0) { 3319e9316f76SJoe Stein zpool_close(zpool_handle); 3320e9dbad6fSeschrock return (-1); 3321e9316f76SJoe Stein } 3322e9316f76SJoe Stein zpool_close(zpool_handle); 3323e9dbad6fSeschrock 3324fa9e4066Sahrens if (type == ZFS_TYPE_VOLUME) { 33255c5460e9Seschrock /* 33265c5460e9Seschrock * If we are creating a volume, the size and block size must 33275c5460e9Seschrock * satisfy a few restraints. First, the blocksize must be a 33285c5460e9Seschrock * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the 33295c5460e9Seschrock * volsize must be a multiple of the block size, and cannot be 33305c5460e9Seschrock * zero. 33315c5460e9Seschrock */ 3332e9dbad6fSeschrock if (props == NULL || nvlist_lookup_uint64(props, 3333e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) { 3334e9dbad6fSeschrock nvlist_free(props); 333599653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3336e9dbad6fSeschrock "missing volume size")); 3337e9dbad6fSeschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 3338fa9e4066Sahrens } 3339fa9e4066Sahrens 3340e9dbad6fSeschrock if ((ret = nvlist_lookup_uint64(props, 3341e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 3342e9dbad6fSeschrock &blocksize)) != 0) { 3343e9dbad6fSeschrock if (ret == ENOENT) { 3344e9dbad6fSeschrock blocksize = zfs_prop_default_numeric( 3345e9dbad6fSeschrock ZFS_PROP_VOLBLOCKSIZE); 3346e9dbad6fSeschrock } else { 3347e9dbad6fSeschrock nvlist_free(props); 334899653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3349e9dbad6fSeschrock "missing volume block size")); 3350e9dbad6fSeschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 3351e9dbad6fSeschrock } 3352e9dbad6fSeschrock } 3353e9dbad6fSeschrock 3354e9dbad6fSeschrock if (size == 0) { 3355e9dbad6fSeschrock nvlist_free(props); 3356e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3357e9dbad6fSeschrock "volume size cannot be zero")); 3358e9dbad6fSeschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 33595c5460e9Seschrock } 33605c5460e9Seschrock 33615c5460e9Seschrock if (size % blocksize != 0) { 3362e9dbad6fSeschrock nvlist_free(props); 336399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3364e9dbad6fSeschrock "volume size must be a multiple of volume block " 3365e9dbad6fSeschrock "size")); 3366e9dbad6fSeschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 3367e9dbad6fSeschrock } 33685c5460e9Seschrock } 33695c5460e9Seschrock 3370fa9e4066Sahrens /* create the dataset */ 33714445fffbSMatthew Ahrens ret = lzc_create(path, ost, props); 33724445fffbSMatthew Ahrens nvlist_free(props); 3373e9dbad6fSeschrock 3374fa9e4066Sahrens /* check for failure */ 3375fa9e4066Sahrens if (ret != 0) { 33769adfa60dSMatthew Ahrens char parent[ZFS_MAX_DATASET_NAME_LEN]; 3377fa9e4066Sahrens (void) parent_name(path, parent, sizeof (parent)); 3378fa9e4066Sahrens 3379fa9e4066Sahrens switch (errno) { 3380fa9e4066Sahrens case ENOENT: 338199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 338299653d4eSeschrock "no such parent '%s'"), parent); 338399653d4eSeschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 3384fa9e4066Sahrens 3385fa9e4066Sahrens case EINVAL: 338699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3387d7d4af51Smmusante "parent '%s' is not a filesystem"), parent); 338899653d4eSeschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 3389fa9e4066Sahrens 339040feaa91Sahrens case ENOTSUP: 339140feaa91Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 339240feaa91Sahrens "pool must be upgraded to set this " 339340feaa91Sahrens "property or value")); 339440feaa91Sahrens return (zfs_error(hdl, EZFS_BADVERSION, errbuf)); 3395fa9e4066Sahrens #ifdef _ILP32 3396fa9e4066Sahrens case EOVERFLOW: 3397fa9e4066Sahrens /* 3398fa9e4066Sahrens * This platform can't address a volume this big. 3399fa9e4066Sahrens */ 340099653d4eSeschrock if (type == ZFS_TYPE_VOLUME) 340199653d4eSeschrock return (zfs_error(hdl, EZFS_VOLTOOBIG, 340299653d4eSeschrock errbuf)); 3403fa9e4066Sahrens #endif 340499653d4eSeschrock /* FALLTHROUGH */ 3405fa9e4066Sahrens default: 340699653d4eSeschrock return (zfs_standard_error(hdl, errno, errbuf)); 3407fa9e4066Sahrens } 3408fa9e4066Sahrens } 3409fa9e4066Sahrens 3410fa9e4066Sahrens return (0); 3411fa9e4066Sahrens } 3412fa9e4066Sahrens 3413fa9e4066Sahrens /* 3414fa9e4066Sahrens * Destroys the given dataset. The caller must make sure that the filesystem 341565fec9f6SChristopher Siden * isn't mounted, and that there are no active dependents. If the file system 341665fec9f6SChristopher Siden * does not exist this function does nothing. 3417fa9e4066Sahrens */ 3418fa9e4066Sahrens int 3419842727c2SChris Kirby zfs_destroy(zfs_handle_t *zhp, boolean_t defer) 3420fa9e4066Sahrens { 3421fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 3422fa9e4066Sahrens 342378f17100SMatthew Ahrens if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) { 342478f17100SMatthew Ahrens nvlist_t *nv = fnvlist_alloc(); 342578f17100SMatthew Ahrens fnvlist_add_boolean(nv, zhp->zfs_name); 342678f17100SMatthew Ahrens int error = lzc_destroy_bookmarks(nv, NULL); 342778f17100SMatthew Ahrens fnvlist_free(nv); 342878f17100SMatthew Ahrens if (error != 0) { 342978f17100SMatthew Ahrens return (zfs_standard_error_fmt(zhp->zfs_hdl, errno, 343078f17100SMatthew Ahrens dgettext(TEXT_DOMAIN, "cannot destroy '%s'"), 343178f17100SMatthew Ahrens zhp->zfs_name)); 343278f17100SMatthew Ahrens } 343378f17100SMatthew Ahrens return (0); 343478f17100SMatthew Ahrens } 343578f17100SMatthew Ahrens 3436fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3437fa9e4066Sahrens 3438e9dbad6fSeschrock if (ZFS_IS_VOLUME(zhp)) { 3439fa9e4066Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3440fa9e4066Sahrens } else { 3441fa9e4066Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3442fa9e4066Sahrens } 3443fa9e4066Sahrens 3444842727c2SChris Kirby zc.zc_defer_destroy = defer; 344565fec9f6SChristopher Siden if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0 && 344665fec9f6SChristopher Siden errno != ENOENT) { 3447ece3d9b3Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, errno, 344899653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot destroy '%s'"), 344999653d4eSeschrock zhp->zfs_name)); 34501d452cf5Sahrens } 3451fa9e4066Sahrens 3452fa9e4066Sahrens remove_mountpoint(zhp); 3453fa9e4066Sahrens 3454fa9e4066Sahrens return (0); 3455fa9e4066Sahrens } 3456fa9e4066Sahrens 34571d452cf5Sahrens struct destroydata { 345819b94df9SMatthew Ahrens nvlist_t *nvl; 345919b94df9SMatthew Ahrens const char *snapname; 34601d452cf5Sahrens }; 34611d452cf5Sahrens 34621d452cf5Sahrens static int 3463681d9761SEric Taylor zfs_check_snap_cb(zfs_handle_t *zhp, void *arg) 34641d452cf5Sahrens { 34651d452cf5Sahrens struct destroydata *dd = arg; 34669adfa60dSMatthew Ahrens char name[ZFS_MAX_DATASET_NAME_LEN]; 3467681d9761SEric Taylor int rv = 0; 34681d452cf5Sahrens 346919b94df9SMatthew Ahrens (void) snprintf(name, sizeof (name), 347019b94df9SMatthew Ahrens "%s@%s", zhp->zfs_name, dd->snapname); 34711d452cf5Sahrens 3472a7a845e4SSteven Hartland if (lzc_exists(name)) 347319b94df9SMatthew Ahrens verify(nvlist_add_boolean(dd->nvl, name) == 0); 34741d452cf5Sahrens 347519b94df9SMatthew Ahrens rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd); 34763ccfa83cSahrens zfs_close(zhp); 34773ccfa83cSahrens return (rv); 34781d452cf5Sahrens } 34791d452cf5Sahrens 34801d452cf5Sahrens /* 34811d452cf5Sahrens * Destroys all snapshots with the given name in zhp & descendants. 34821d452cf5Sahrens */ 34831d452cf5Sahrens int 3484842727c2SChris Kirby zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer) 34851d452cf5Sahrens { 34861d452cf5Sahrens int ret; 34871d452cf5Sahrens struct destroydata dd = { 0 }; 34881d452cf5Sahrens 34891d452cf5Sahrens dd.snapname = snapname; 349019b94df9SMatthew Ahrens verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0); 349119b94df9SMatthew Ahrens (void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd); 34921d452cf5Sahrens 3493a7a845e4SSteven Hartland if (nvlist_empty(dd.nvl)) { 349419b94df9SMatthew Ahrens ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT, 34951d452cf5Sahrens dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"), 349619b94df9SMatthew Ahrens zhp->zfs_name, snapname); 349719b94df9SMatthew Ahrens } else { 34983b2aab18SMatthew Ahrens ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer); 349919b94df9SMatthew Ahrens } 350019b94df9SMatthew Ahrens nvlist_free(dd.nvl); 350119b94df9SMatthew Ahrens return (ret); 3502e5351341SMatthew Ahrens } 3503e5351341SMatthew Ahrens 350419b94df9SMatthew Ahrens /* 35053b2aab18SMatthew Ahrens * Destroys all the snapshots named in the nvlist. 350619b94df9SMatthew Ahrens */ 350719b94df9SMatthew Ahrens int 35083b2aab18SMatthew Ahrens zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer) 350919b94df9SMatthew Ahrens { 351019b94df9SMatthew Ahrens int ret; 35114cde22c2SChris Williamson nvlist_t *errlist = NULL; 351219b94df9SMatthew Ahrens 35134445fffbSMatthew Ahrens ret = lzc_destroy_snaps(snaps, defer, &errlist); 35141d452cf5Sahrens 35154cde22c2SChris Williamson if (ret == 0) { 35164cde22c2SChris Williamson nvlist_free(errlist); 35173b2aab18SMatthew Ahrens return (0); 35184cde22c2SChris Williamson } 35193b2aab18SMatthew Ahrens 3520a7a845e4SSteven Hartland if (nvlist_empty(errlist)) { 35213b2aab18SMatthew Ahrens char errbuf[1024]; 35223b2aab18SMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), 35233b2aab18SMatthew Ahrens dgettext(TEXT_DOMAIN, "cannot destroy snapshots")); 35243b2aab18SMatthew Ahrens 35253b2aab18SMatthew Ahrens ret = zfs_standard_error(hdl, ret, errbuf); 35263b2aab18SMatthew Ahrens } 35274445fffbSMatthew Ahrens for (nvpair_t *pair = nvlist_next_nvpair(errlist, NULL); 35284445fffbSMatthew Ahrens pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) { 35291d452cf5Sahrens char errbuf[1024]; 35304445fffbSMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), 35314445fffbSMatthew Ahrens dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"), 35324445fffbSMatthew Ahrens nvpair_name(pair)); 35331d452cf5Sahrens 35344445fffbSMatthew Ahrens switch (fnvpair_value_int32(pair)) { 35351d452cf5Sahrens case EEXIST: 35363b2aab18SMatthew Ahrens zfs_error_aux(hdl, 35373b2aab18SMatthew Ahrens dgettext(TEXT_DOMAIN, "snapshot is cloned")); 35383b2aab18SMatthew Ahrens ret = zfs_error(hdl, EZFS_EXISTS, errbuf); 35394445fffbSMatthew Ahrens break; 35401d452cf5Sahrens default: 35413b2aab18SMatthew Ahrens ret = zfs_standard_error(hdl, errno, errbuf); 35424445fffbSMatthew Ahrens break; 35434445fffbSMatthew Ahrens } 35441d452cf5Sahrens } 35451d452cf5Sahrens 35464cde22c2SChris Williamson nvlist_free(errlist); 35474445fffbSMatthew Ahrens return (ret); 35481d452cf5Sahrens } 35491d452cf5Sahrens 3550fa9e4066Sahrens /* 3551fa9e4066Sahrens * Clones the given dataset. The target must be of the same type as the source. 3552fa9e4066Sahrens */ 3553fa9e4066Sahrens int 3554e9dbad6fSeschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props) 3555fa9e4066Sahrens { 35569adfa60dSMatthew Ahrens char parent[ZFS_MAX_DATASET_NAME_LEN]; 3557fa9e4066Sahrens int ret; 355899653d4eSeschrock char errbuf[1024]; 355999653d4eSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 3560e9dbad6fSeschrock uint64_t zoned; 3561fa9e4066Sahrens 3562fa9e4066Sahrens assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 3563fa9e4066Sahrens 356499653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 356599653d4eSeschrock "cannot create '%s'"), target); 356699653d4eSeschrock 356719b94df9SMatthew Ahrens /* validate the target/clone name */ 3568f18faf3fSek110237 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE)) 356999653d4eSeschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3570fa9e4066Sahrens 3571fa9e4066Sahrens /* validate parents exist */ 35727f1f55eaSvb160487 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0) 3573fa9e4066Sahrens return (-1); 3574fa9e4066Sahrens 3575fa9e4066Sahrens (void) parent_name(target, parent, sizeof (parent)); 3576fa9e4066Sahrens 3577fa9e4066Sahrens /* do the clone */ 3578e9dbad6fSeschrock 3579e9dbad6fSeschrock if (props) { 35804445fffbSMatthew Ahrens zfs_type_t type; 35814445fffbSMatthew Ahrens if (ZFS_IS_VOLUME(zhp)) { 35824445fffbSMatthew Ahrens type = ZFS_TYPE_VOLUME; 35834445fffbSMatthew Ahrens } else { 35844445fffbSMatthew Ahrens type = ZFS_TYPE_FILESYSTEM; 35854445fffbSMatthew Ahrens } 35860a48a24eStimh if ((props = zfs_valid_proplist(hdl, type, props, zoned, 3587e9316f76SJoe Stein zhp, zhp->zpool_hdl, errbuf)) == NULL) 3588e9dbad6fSeschrock return (-1); 3589e9dbad6fSeschrock } 3590e9dbad6fSeschrock 35914445fffbSMatthew Ahrens ret = lzc_clone(target, zhp->zfs_name, props); 3592e9dbad6fSeschrock nvlist_free(props); 3593e9dbad6fSeschrock 3594fa9e4066Sahrens if (ret != 0) { 3595fa9e4066Sahrens switch (errno) { 3596fa9e4066Sahrens 3597fa9e4066Sahrens case ENOENT: 3598fa9e4066Sahrens /* 3599fa9e4066Sahrens * The parent doesn't exist. We should have caught this 3600fa9e4066Sahrens * above, but there may a race condition that has since 3601fa9e4066Sahrens * destroyed the parent. 3602fa9e4066Sahrens * 3603fa9e4066Sahrens * At this point, we don't know whether it's the source 3604fa9e4066Sahrens * that doesn't exist anymore, or whether the target 3605fa9e4066Sahrens * dataset doesn't exist. 3606fa9e4066Sahrens */ 360799653d4eSeschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 360899653d4eSeschrock "no such parent '%s'"), parent); 360999653d4eSeschrock return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf)); 3610fa9e4066Sahrens 361199653d4eSeschrock case EXDEV: 361299653d4eSeschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 361399653d4eSeschrock "source and target pools differ")); 361499653d4eSeschrock return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET, 361599653d4eSeschrock errbuf)); 361699653d4eSeschrock 361799653d4eSeschrock default: 361899653d4eSeschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 361999653d4eSeschrock errbuf)); 362099653d4eSeschrock } 362199653d4eSeschrock } 362299653d4eSeschrock 362399653d4eSeschrock return (ret); 362499653d4eSeschrock } 362599653d4eSeschrock 362699653d4eSeschrock /* 362799653d4eSeschrock * Promotes the given clone fs to be the clone parent. 362899653d4eSeschrock */ 362999653d4eSeschrock int 363099653d4eSeschrock zfs_promote(zfs_handle_t *zhp) 363199653d4eSeschrock { 363299653d4eSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 363399653d4eSeschrock zfs_cmd_t zc = { 0 }; 363499653d4eSeschrock char parent[MAXPATHLEN]; 363599653d4eSeschrock int ret; 363699653d4eSeschrock char errbuf[1024]; 363799653d4eSeschrock 363899653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 363999653d4eSeschrock "cannot promote '%s'"), zhp->zfs_name); 364099653d4eSeschrock 364199653d4eSeschrock if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 364299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 364399653d4eSeschrock "snapshots can not be promoted")); 364499653d4eSeschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 364599653d4eSeschrock } 364699653d4eSeschrock 36473cb34c60Sahrens (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent)); 364899653d4eSeschrock if (parent[0] == '\0') { 364999653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 365099653d4eSeschrock "not a cloned filesystem")); 365199653d4eSeschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 365299653d4eSeschrock } 365399653d4eSeschrock 36543cb34c60Sahrens (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin, 3655e9dbad6fSeschrock sizeof (zc.zc_value)); 365699653d4eSeschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3657ecd6cf80Smarks ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc); 365899653d4eSeschrock 365999653d4eSeschrock if (ret != 0) { 36600b69c2f0Sahrens int save_errno = errno; 3661fa9e4066Sahrens 36620b69c2f0Sahrens switch (save_errno) { 3663fa9e4066Sahrens case EEXIST: 3664681d9761SEric Taylor /* There is a conflicting snapshot name. */ 366599653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3666681d9761SEric Taylor "conflicting snapshot '%s' from parent '%s'"), 3667681d9761SEric Taylor zc.zc_string, parent); 366899653d4eSeschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3669fa9e4066Sahrens 3670fa9e4066Sahrens default: 36710b69c2f0Sahrens return (zfs_standard_error(hdl, save_errno, errbuf)); 3672fa9e4066Sahrens } 3673fa9e4066Sahrens } 3674e9dbad6fSeschrock return (ret); 36751d452cf5Sahrens } 36761d452cf5Sahrens 36774445fffbSMatthew Ahrens typedef struct snapdata { 36784445fffbSMatthew Ahrens nvlist_t *sd_nvl; 36794445fffbSMatthew Ahrens const char *sd_snapname; 36804445fffbSMatthew Ahrens } snapdata_t; 36814445fffbSMatthew Ahrens 36824445fffbSMatthew Ahrens static int 36834445fffbSMatthew Ahrens zfs_snapshot_cb(zfs_handle_t *zhp, void *arg) 36844445fffbSMatthew Ahrens { 36854445fffbSMatthew Ahrens snapdata_t *sd = arg; 36869adfa60dSMatthew Ahrens char name[ZFS_MAX_DATASET_NAME_LEN]; 36874445fffbSMatthew Ahrens int rv = 0; 36884445fffbSMatthew Ahrens 3689ca48f36fSKeith M Wesolowski if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) { 36904445fffbSMatthew Ahrens (void) snprintf(name, sizeof (name), 36914445fffbSMatthew Ahrens "%s@%s", zfs_get_name(zhp), sd->sd_snapname); 36924445fffbSMatthew Ahrens 36934445fffbSMatthew Ahrens fnvlist_add_boolean(sd->sd_nvl, name); 36944445fffbSMatthew Ahrens 36954445fffbSMatthew Ahrens rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd); 3696ca48f36fSKeith M Wesolowski } 36974445fffbSMatthew Ahrens zfs_close(zhp); 3698ca48f36fSKeith M Wesolowski 36994445fffbSMatthew Ahrens return (rv); 37004445fffbSMatthew Ahrens } 37014445fffbSMatthew Ahrens 3702fa9e4066Sahrens /* 37034445fffbSMatthew Ahrens * Creates snapshots. The keys in the snaps nvlist are the snapshots to be 37044445fffbSMatthew Ahrens * created. 3705fa9e4066Sahrens */ 3706fa9e4066Sahrens int 37074445fffbSMatthew Ahrens zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props) 37084445fffbSMatthew Ahrens { 37094445fffbSMatthew Ahrens int ret; 37104445fffbSMatthew Ahrens char errbuf[1024]; 37114445fffbSMatthew Ahrens nvpair_t *elem; 37124445fffbSMatthew Ahrens nvlist_t *errors; 37134445fffbSMatthew Ahrens 37144445fffbSMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 37154445fffbSMatthew Ahrens "cannot create snapshots ")); 37164445fffbSMatthew Ahrens 37174445fffbSMatthew Ahrens elem = NULL; 37184445fffbSMatthew Ahrens while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) { 37194445fffbSMatthew Ahrens const char *snapname = nvpair_name(elem); 37204445fffbSMatthew Ahrens 37214445fffbSMatthew Ahrens /* validate the target name */ 37224445fffbSMatthew Ahrens if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT, 37234445fffbSMatthew Ahrens B_TRUE)) { 37244445fffbSMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), 37254445fffbSMatthew Ahrens dgettext(TEXT_DOMAIN, 37264445fffbSMatthew Ahrens "cannot create snapshot '%s'"), snapname); 37274445fffbSMatthew Ahrens return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 37284445fffbSMatthew Ahrens } 37294445fffbSMatthew Ahrens } 37304445fffbSMatthew Ahrens 3731e9316f76SJoe Stein /* 3732e9316f76SJoe Stein * get pool handle for prop validation. assumes all snaps are in the 3733e9316f76SJoe Stein * same pool, as does lzc_snapshot (below). 3734e9316f76SJoe Stein */ 37359adfa60dSMatthew Ahrens char pool[ZFS_MAX_DATASET_NAME_LEN]; 3736e9316f76SJoe Stein elem = nvlist_next_nvpair(snaps, NULL); 3737e9316f76SJoe Stein (void) strlcpy(pool, nvpair_name(elem), sizeof (pool)); 3738e9316f76SJoe Stein pool[strcspn(pool, "/@")] = '\0'; 3739e9316f76SJoe Stein zpool_handle_t *zpool_hdl = zpool_open(hdl, pool); 3740e9316f76SJoe Stein 37414445fffbSMatthew Ahrens if (props != NULL && 37424445fffbSMatthew Ahrens (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT, 3743e9316f76SJoe Stein props, B_FALSE, NULL, zpool_hdl, errbuf)) == NULL) { 3744e9316f76SJoe Stein zpool_close(zpool_hdl); 37454445fffbSMatthew Ahrens return (-1); 37464445fffbSMatthew Ahrens } 3747e9316f76SJoe Stein zpool_close(zpool_hdl); 37484445fffbSMatthew Ahrens 37494445fffbSMatthew Ahrens ret = lzc_snapshot(snaps, props, &errors); 37504445fffbSMatthew Ahrens 37514445fffbSMatthew Ahrens if (ret != 0) { 37524445fffbSMatthew Ahrens boolean_t printed = B_FALSE; 37534445fffbSMatthew Ahrens for (elem = nvlist_next_nvpair(errors, NULL); 37544445fffbSMatthew Ahrens elem != NULL; 37554445fffbSMatthew Ahrens elem = nvlist_next_nvpair(errors, elem)) { 37564445fffbSMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), 37574445fffbSMatthew Ahrens dgettext(TEXT_DOMAIN, 37584445fffbSMatthew Ahrens "cannot create snapshot '%s'"), nvpair_name(elem)); 37594445fffbSMatthew Ahrens (void) zfs_standard_error(hdl, 37604445fffbSMatthew Ahrens fnvpair_value_int32(elem), errbuf); 37614445fffbSMatthew Ahrens printed = B_TRUE; 37624445fffbSMatthew Ahrens } 37634445fffbSMatthew Ahrens if (!printed) { 37644445fffbSMatthew Ahrens switch (ret) { 37654445fffbSMatthew Ahrens case EXDEV: 37664445fffbSMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37674445fffbSMatthew Ahrens "multiple snapshots of same " 37684445fffbSMatthew Ahrens "fs not allowed")); 37694445fffbSMatthew Ahrens (void) zfs_error(hdl, EZFS_EXISTS, errbuf); 37704445fffbSMatthew Ahrens 37714445fffbSMatthew Ahrens break; 37724445fffbSMatthew Ahrens default: 37734445fffbSMatthew Ahrens (void) zfs_standard_error(hdl, ret, errbuf); 37744445fffbSMatthew Ahrens } 37754445fffbSMatthew Ahrens } 37764445fffbSMatthew Ahrens } 37774445fffbSMatthew Ahrens 37784445fffbSMatthew Ahrens nvlist_free(props); 37794445fffbSMatthew Ahrens nvlist_free(errors); 37804445fffbSMatthew Ahrens return (ret); 37814445fffbSMatthew Ahrens } 37824445fffbSMatthew Ahrens 37834445fffbSMatthew Ahrens int 3784bb0ade09Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive, 3785bb0ade09Sahrens nvlist_t *props) 3786fa9e4066Sahrens { 3787fa9e4066Sahrens int ret; 37884445fffbSMatthew Ahrens snapdata_t sd = { 0 }; 37899adfa60dSMatthew Ahrens char fsname[ZFS_MAX_DATASET_NAME_LEN]; 37904445fffbSMatthew Ahrens char *cp; 37914445fffbSMatthew Ahrens zfs_handle_t *zhp; 379299653d4eSeschrock char errbuf[1024]; 3793fa9e4066Sahrens 379499653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 37954445fffbSMatthew Ahrens "cannot snapshot %s"), path); 379699653d4eSeschrock 3797f18faf3fSek110237 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE)) 379899653d4eSeschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3799fa9e4066Sahrens 38004445fffbSMatthew Ahrens (void) strlcpy(fsname, path, sizeof (fsname)); 38014445fffbSMatthew Ahrens cp = strchr(fsname, '@'); 38024445fffbSMatthew Ahrens *cp = '\0'; 38034445fffbSMatthew Ahrens sd.sd_snapname = cp + 1; 3804bb0ade09Sahrens 38054445fffbSMatthew Ahrens if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | 3806fa9e4066Sahrens ZFS_TYPE_VOLUME)) == NULL) { 3807fa9e4066Sahrens return (-1); 3808fa9e4066Sahrens } 3809fa9e4066Sahrens 38104445fffbSMatthew Ahrens verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0); 38114445fffbSMatthew Ahrens if (recursive) { 38124445fffbSMatthew Ahrens (void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd); 38134445fffbSMatthew Ahrens } else { 38144445fffbSMatthew Ahrens fnvlist_add_boolean(sd.sd_nvl, path); 3815681d9761SEric Taylor } 3816fa9e4066Sahrens 38174445fffbSMatthew Ahrens ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props); 38184445fffbSMatthew Ahrens nvlist_free(sd.sd_nvl); 3819fa9e4066Sahrens zfs_close(zhp); 3820fa9e4066Sahrens return (ret); 3821fa9e4066Sahrens } 3822fa9e4066Sahrens 3823fa9e4066Sahrens /* 3824b12a1c38Slling * Destroy any more recent snapshots. We invoke this callback on any dependents 3825b12a1c38Slling * of the snapshot first. If the 'cb_dependent' member is non-zero, then this 3826b12a1c38Slling * is a dependent and we should just destroy it without checking the transaction 3827b12a1c38Slling * group. 3828fa9e4066Sahrens */ 3829b12a1c38Slling typedef struct rollback_data { 3830b12a1c38Slling const char *cb_target; /* the snapshot */ 3831b12a1c38Slling uint64_t cb_create; /* creation time reference */ 3832c391e322Sahrens boolean_t cb_error; 3833c391e322Sahrens boolean_t cb_force; 3834b12a1c38Slling } rollback_data_t; 3835b12a1c38Slling 3836b12a1c38Slling static int 383778f17100SMatthew Ahrens rollback_destroy_dependent(zfs_handle_t *zhp, void *data) 3838b12a1c38Slling { 3839b12a1c38Slling rollback_data_t *cbp = data; 3840c391e322Sahrens prop_changelist_t *clp; 3841c391e322Sahrens 384278f17100SMatthew Ahrens /* We must destroy this clone; first unmount it */ 38430069fd67STim Haley clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 3844c391e322Sahrens cbp->cb_force ? MS_FORCE: 0); 3845c391e322Sahrens if (clp == NULL || changelist_prefix(clp) != 0) { 3846c391e322Sahrens cbp->cb_error = B_TRUE; 3847c391e322Sahrens zfs_close(zhp); 3848c391e322Sahrens return (0); 3849c391e322Sahrens } 3850842727c2SChris Kirby if (zfs_destroy(zhp, B_FALSE) != 0) 3851c391e322Sahrens cbp->cb_error = B_TRUE; 3852c391e322Sahrens else 3853c391e322Sahrens changelist_remove(clp, zhp->zfs_name); 3854ba7b046eSahrens (void) changelist_postfix(clp); 3855c391e322Sahrens changelist_free(clp); 385678f17100SMatthew Ahrens 385778f17100SMatthew Ahrens zfs_close(zhp); 385878f17100SMatthew Ahrens return (0); 385978f17100SMatthew Ahrens } 386078f17100SMatthew Ahrens 386178f17100SMatthew Ahrens static int 386278f17100SMatthew Ahrens rollback_destroy(zfs_handle_t *zhp, void *data) 386378f17100SMatthew Ahrens { 386478f17100SMatthew Ahrens rollback_data_t *cbp = data; 386578f17100SMatthew Ahrens 386678f17100SMatthew Ahrens if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) { 386778f17100SMatthew Ahrens cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE, 386878f17100SMatthew Ahrens rollback_destroy_dependent, cbp); 386978f17100SMatthew Ahrens 387078f17100SMatthew Ahrens cbp->cb_error |= zfs_destroy(zhp, B_FALSE); 3871b12a1c38Slling } 3872b12a1c38Slling 3873b12a1c38Slling zfs_close(zhp); 3874b12a1c38Slling return (0); 3875b12a1c38Slling } 3876b12a1c38Slling 3877b12a1c38Slling /* 38784ccbb6e7Sahrens * Given a dataset, rollback to a specific snapshot, discarding any 38794ccbb6e7Sahrens * data changes since then and making it the active dataset. 38804ccbb6e7Sahrens * 388178f17100SMatthew Ahrens * Any snapshots and bookmarks more recent than the target are 388278f17100SMatthew Ahrens * destroyed, along with their dependents (i.e. clones). 3883b12a1c38Slling */ 38844ccbb6e7Sahrens int 3885c391e322Sahrens zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force) 3886fa9e4066Sahrens { 38874ccbb6e7Sahrens rollback_data_t cb = { 0 }; 38884ccbb6e7Sahrens int err; 38897b97dc1aSrm160521 boolean_t restore_resv = 0; 3890f83b46baSPaul Dagnelie uint64_t old_volsize = 0, new_volsize; 38917b97dc1aSrm160521 zfs_prop_t resv_prop; 3892fa9e4066Sahrens 3893fa9e4066Sahrens assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM || 3894fa9e4066Sahrens zhp->zfs_type == ZFS_TYPE_VOLUME); 3895fa9e4066Sahrens 38964ccbb6e7Sahrens /* 38972e2c1355SMatthew Ahrens * Destroy all recent snapshots and their dependents. 38984ccbb6e7Sahrens */ 3899c391e322Sahrens cb.cb_force = force; 39004ccbb6e7Sahrens cb.cb_target = snap->zfs_name; 39014ccbb6e7Sahrens cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG); 39020d8fa8f8SMartin Matuska (void) zfs_iter_snapshots(zhp, B_FALSE, rollback_destroy, &cb); 390378f17100SMatthew Ahrens (void) zfs_iter_bookmarks(zhp, rollback_destroy, &cb); 39044ccbb6e7Sahrens 3905c391e322Sahrens if (cb.cb_error) 3906c391e322Sahrens return (-1); 39074ccbb6e7Sahrens 39084ccbb6e7Sahrens /* 39094ccbb6e7Sahrens * Now that we have verified that the snapshot is the latest, 39104ccbb6e7Sahrens * rollback to the given snapshot. 39114ccbb6e7Sahrens */ 39124ccbb6e7Sahrens 39137b97dc1aSrm160521 if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 39147b97dc1aSrm160521 if (zfs_which_resv_prop(zhp, &resv_prop) < 0) 39157b97dc1aSrm160521 return (-1); 39167b97dc1aSrm160521 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 39177b97dc1aSrm160521 restore_resv = 39187b97dc1aSrm160521 (old_volsize == zfs_prop_get_int(zhp, resv_prop)); 39197b97dc1aSrm160521 } 3920fa9e4066Sahrens 3921fa9e4066Sahrens /* 39224ccbb6e7Sahrens * We rely on zfs_iter_children() to verify that there are no 39234ccbb6e7Sahrens * newer snapshots for the given dataset. Therefore, we can 39244ccbb6e7Sahrens * simply pass the name on to the ioctl() call. There is still 39254ccbb6e7Sahrens * an unlikely race condition where the user has taken a 39264ccbb6e7Sahrens * snapshot since we verified that this was the most recent. 3927fa9e4066Sahrens */ 3928a7027df1SMatthew Ahrens err = lzc_rollback(zhp->zfs_name, NULL, 0); 3929a7027df1SMatthew Ahrens if (err != 0) { 3930ece3d9b3Slling (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno, 393199653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot rollback '%s'"), 393299653d4eSeschrock zhp->zfs_name); 3933b9415e83Srm160521 return (err); 3934b9415e83Srm160521 } 3935fa9e4066Sahrens 39367b97dc1aSrm160521 /* 39377b97dc1aSrm160521 * For volumes, if the pre-rollback volsize matched the pre- 39387b97dc1aSrm160521 * rollback reservation and the volsize has changed then set 39397b97dc1aSrm160521 * the reservation property to the post-rollback volsize. 39407b97dc1aSrm160521 * Make a new handle since the rollback closed the dataset. 39417b97dc1aSrm160521 */ 3942b9415e83Srm160521 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) && 3943b9415e83Srm160521 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) { 39447b97dc1aSrm160521 if (restore_resv) { 39457b97dc1aSrm160521 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 39467b97dc1aSrm160521 if (old_volsize != new_volsize) 3947b9415e83Srm160521 err = zfs_prop_set_int(zhp, resv_prop, 3948b9415e83Srm160521 new_volsize); 39497b97dc1aSrm160521 } 39507b97dc1aSrm160521 zfs_close(zhp); 39517b97dc1aSrm160521 } 39524ccbb6e7Sahrens return (err); 3953b12a1c38Slling } 3954b12a1c38Slling 3955b12a1c38Slling /* 3956fa9e4066Sahrens * Renames the given dataset. 3957fa9e4066Sahrens */ 3958fa9e4066Sahrens int 39596a9cb0eaSEric Schrock zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive, 39606a9cb0eaSEric Schrock boolean_t force_unmount) 3961fa9e4066Sahrens { 396288f61deeSIgor Kozhukhov int ret = 0; 3963fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 3964fa9e4066Sahrens char *delim; 3965cdf5b4caSmmusante prop_changelist_t *cl = NULL; 3966cdf5b4caSmmusante zfs_handle_t *zhrp = NULL; 3967cdf5b4caSmmusante char *parentname = NULL; 39689adfa60dSMatthew Ahrens char parent[ZFS_MAX_DATASET_NAME_LEN]; 396999653d4eSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 397099653d4eSeschrock char errbuf[1024]; 3971fa9e4066Sahrens 3972fa9e4066Sahrens /* if we have the same exact name, just return success */ 3973fa9e4066Sahrens if (strcmp(zhp->zfs_name, target) == 0) 3974fa9e4066Sahrens return (0); 3975fa9e4066Sahrens 397699653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 397799653d4eSeschrock "cannot rename to '%s'"), target); 397899653d4eSeschrock 3979fa9e4066Sahrens /* 3980fa9e4066Sahrens * Make sure the target name is valid 3981fa9e4066Sahrens */ 3982fa9e4066Sahrens if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 398398579b20Snd150628 if ((strchr(target, '@') == NULL) || 398498579b20Snd150628 *target == '@') { 398598579b20Snd150628 /* 398698579b20Snd150628 * Snapshot target name is abbreviated, 398798579b20Snd150628 * reconstruct full dataset name 398898579b20Snd150628 */ 398998579b20Snd150628 (void) strlcpy(parent, zhp->zfs_name, 399098579b20Snd150628 sizeof (parent)); 399198579b20Snd150628 delim = strchr(parent, '@'); 399298579b20Snd150628 if (strchr(target, '@') == NULL) 399398579b20Snd150628 *(++delim) = '\0'; 399498579b20Snd150628 else 399598579b20Snd150628 *delim = '\0'; 399698579b20Snd150628 (void) strlcat(parent, target, sizeof (parent)); 399798579b20Snd150628 target = parent; 399898579b20Snd150628 } else { 3999fa9e4066Sahrens /* 4000fa9e4066Sahrens * Make sure we're renaming within the same dataset. 4001fa9e4066Sahrens */ 400298579b20Snd150628 delim = strchr(target, '@'); 400398579b20Snd150628 if (strncmp(zhp->zfs_name, target, delim - target) 400498579b20Snd150628 != 0 || zhp->zfs_name[delim - target] != '@') { 400599653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 400698579b20Snd150628 "snapshots must be part of same " 400798579b20Snd150628 "dataset")); 400898579b20Snd150628 return (zfs_error(hdl, EZFS_CROSSTARGET, 400998579b20Snd150628 errbuf)); 4010fa9e4066Sahrens } 401198579b20Snd150628 } 4012f18faf3fSek110237 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 401398579b20Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 4014fa9e4066Sahrens } else { 4015cdf5b4caSmmusante if (recursive) { 4016cdf5b4caSmmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4017cdf5b4caSmmusante "recursive rename must be a snapshot")); 4018cdf5b4caSmmusante return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 4019cdf5b4caSmmusante } 4020cdf5b4caSmmusante 4021f18faf3fSek110237 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 402298579b20Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 4023e9dbad6fSeschrock 4024fa9e4066Sahrens /* validate parents */ 4025d41c4376SMark J Musante if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0) 4026fa9e4066Sahrens return (-1); 4027fa9e4066Sahrens 4028fa9e4066Sahrens /* make sure we're in the same pool */ 4029fa9e4066Sahrens verify((delim = strchr(target, '/')) != NULL); 4030fa9e4066Sahrens if (strncmp(zhp->zfs_name, target, delim - target) != 0 || 4031fa9e4066Sahrens zhp->zfs_name[delim - target] != '/') { 403299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 403399653d4eSeschrock "datasets must be within same pool")); 403499653d4eSeschrock return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 4035fa9e4066Sahrens } 4036f2fdf992Snd150628 4037f2fdf992Snd150628 /* new name cannot be a child of the current dataset name */ 4038d41c4376SMark J Musante if (is_descendant(zhp->zfs_name, target)) { 4039f2fdf992Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4040d41c4376SMark J Musante "New dataset name cannot be a descendant of " 4041f2fdf992Snd150628 "current dataset name")); 4042f2fdf992Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 4043f2fdf992Snd150628 } 4044fa9e4066Sahrens } 4045fa9e4066Sahrens 404699653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), 404799653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name); 404899653d4eSeschrock 4049fa9e4066Sahrens if (getzoneid() == GLOBAL_ZONEID && 4050fa9e4066Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 405199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 405299653d4eSeschrock "dataset is used in a non-global zone")); 405399653d4eSeschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 4054fa9e4066Sahrens } 4055fa9e4066Sahrens 4056cdf5b4caSmmusante if (recursive) { 4057f0c5ee21Smmusante parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name); 4058f0c5ee21Smmusante if (parentname == NULL) { 4059f0c5ee21Smmusante ret = -1; 4060f0c5ee21Smmusante goto error; 4061f0c5ee21Smmusante } 4062cdf5b4caSmmusante delim = strchr(parentname, '@'); 4063cdf5b4caSmmusante *delim = '\0'; 4064990b4856Slling zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET); 4065cdf5b4caSmmusante if (zhrp == NULL) { 4066f0c5ee21Smmusante ret = -1; 4067f0c5ee21Smmusante goto error; 4068cdf5b4caSmmusante } 406933cde0d0SMatthew Ahrens } else if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) { 40706a9cb0eaSEric Schrock if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0, 40716a9cb0eaSEric Schrock force_unmount ? MS_FORCE : 0)) == NULL) 407299653d4eSeschrock return (-1); 4073fa9e4066Sahrens 4074fa9e4066Sahrens if (changelist_haszonedchild(cl)) { 407599653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 407699653d4eSeschrock "child dataset with inherited mountpoint is used " 407799653d4eSeschrock "in a non-global zone")); 4078e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_ZONED, errbuf); 4079f83b46baSPaul Dagnelie ret = -1; 4080fa9e4066Sahrens goto error; 4081fa9e4066Sahrens } 4082fa9e4066Sahrens 4083fa9e4066Sahrens if ((ret = changelist_prefix(cl)) != 0) 4084fa9e4066Sahrens goto error; 4085cdf5b4caSmmusante } 4086fa9e4066Sahrens 4087e9dbad6fSeschrock if (ZFS_IS_VOLUME(zhp)) 4088fa9e4066Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 4089fa9e4066Sahrens else 4090fa9e4066Sahrens zc.zc_objset_type = DMU_OST_ZFS; 4091fa9e4066Sahrens 409298579b20Snd150628 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 4093e9dbad6fSeschrock (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value)); 409498579b20Snd150628 4095cdf5b4caSmmusante zc.zc_cookie = recursive; 4096cdf5b4caSmmusante 4097ecd6cf80Smarks if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) { 4098cdf5b4caSmmusante /* 4099cdf5b4caSmmusante * if it was recursive, the one that actually failed will 4100cdf5b4caSmmusante * be in zc.zc_name 4101cdf5b4caSmmusante */ 4102cdf5b4caSmmusante (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 41033cb34c60Sahrens "cannot rename '%s'"), zc.zc_name); 4104cdf5b4caSmmusante 4105cdf5b4caSmmusante if (recursive && errno == EEXIST) { 4106cdf5b4caSmmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4107cdf5b4caSmmusante "a child dataset already has a snapshot " 4108cdf5b4caSmmusante "with the new name")); 4109a10acbd6Seschrock (void) zfs_error(hdl, EZFS_EXISTS, errbuf); 4110cdf5b4caSmmusante } else { 411199653d4eSeschrock (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf); 4112cdf5b4caSmmusante } 4113fa9e4066Sahrens 4114fa9e4066Sahrens /* 4115fa9e4066Sahrens * On failure, we still want to remount any filesystems that 4116fa9e4066Sahrens * were previously mounted, so we don't alter the system state. 4117fa9e4066Sahrens */ 411833cde0d0SMatthew Ahrens if (cl != NULL) 4119fa9e4066Sahrens (void) changelist_postfix(cl); 4120cdf5b4caSmmusante } else { 412133cde0d0SMatthew Ahrens if (cl != NULL) { 4122fa9e4066Sahrens changelist_rename(cl, zfs_get_name(zhp), target); 4123fa9e4066Sahrens ret = changelist_postfix(cl); 4124fa9e4066Sahrens } 4125cdf5b4caSmmusante } 4126fa9e4066Sahrens 4127fa9e4066Sahrens error: 412833cde0d0SMatthew Ahrens if (parentname != NULL) { 4129cdf5b4caSmmusante free(parentname); 4130cdf5b4caSmmusante } 413133cde0d0SMatthew Ahrens if (zhrp != NULL) { 4132cdf5b4caSmmusante zfs_close(zhrp); 4133cdf5b4caSmmusante } 413433cde0d0SMatthew Ahrens if (cl != NULL) { 4135fa9e4066Sahrens changelist_free(cl); 4136cdf5b4caSmmusante } 4137fa9e4066Sahrens return (ret); 4138fa9e4066Sahrens } 4139fa9e4066Sahrens 4140e9dbad6fSeschrock nvlist_t * 4141e9dbad6fSeschrock zfs_get_user_props(zfs_handle_t *zhp) 4142e9dbad6fSeschrock { 4143e9dbad6fSeschrock return (zhp->zfs_user_props); 4144e9dbad6fSeschrock } 4145e9dbad6fSeschrock 414692241e0bSTom Erickson nvlist_t * 414792241e0bSTom Erickson zfs_get_recvd_props(zfs_handle_t *zhp) 414892241e0bSTom Erickson { 414992241e0bSTom Erickson if (zhp->zfs_recvd_props == NULL) 415092241e0bSTom Erickson if (get_recvd_props_ioctl(zhp) != 0) 415192241e0bSTom Erickson return (NULL); 415292241e0bSTom Erickson return (zhp->zfs_recvd_props); 415392241e0bSTom Erickson } 415492241e0bSTom Erickson 4155e9dbad6fSeschrock /* 4156e9dbad6fSeschrock * This function is used by 'zfs list' to determine the exact set of columns to 4157e9dbad6fSeschrock * display, and their maximum widths. This does two main things: 4158e9dbad6fSeschrock * 4159e9dbad6fSeschrock * - If this is a list of all properties, then expand the list to include 4160e9dbad6fSeschrock * all native properties, and set a flag so that for each dataset we look 4161e9dbad6fSeschrock * for new unique user properties and add them to the list. 4162e9dbad6fSeschrock * 4163e9dbad6fSeschrock * - For non fixed-width properties, keep track of the maximum width seen 416492241e0bSTom Erickson * so that we can size the column appropriately. If the user has 416592241e0bSTom Erickson * requested received property values, we also need to compute the width 416692241e0bSTom Erickson * of the RECEIVED column. 4167e9dbad6fSeschrock */ 4168e9dbad6fSeschrock int 416943d68d68SYuri Pankov zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received, 417043d68d68SYuri Pankov boolean_t literal) 4171e9dbad6fSeschrock { 4172e9dbad6fSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 4173990b4856Slling zprop_list_t *entry; 4174990b4856Slling zprop_list_t **last, **start; 4175e9dbad6fSeschrock nvlist_t *userprops, *propval; 4176e9dbad6fSeschrock nvpair_t *elem; 4177e9dbad6fSeschrock char *strval; 4178e9dbad6fSeschrock char buf[ZFS_MAXPROPLEN]; 4179e9dbad6fSeschrock 4180990b4856Slling if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0) 4181e9dbad6fSeschrock return (-1); 4182e9dbad6fSeschrock 4183e9dbad6fSeschrock userprops = zfs_get_user_props(zhp); 4184e9dbad6fSeschrock 4185e9dbad6fSeschrock entry = *plp; 4186e9dbad6fSeschrock if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) { 4187e9dbad6fSeschrock /* 4188e9dbad6fSeschrock * Go through and add any user properties as necessary. We 4189e9dbad6fSeschrock * start by incrementing our list pointer to the first 4190e9dbad6fSeschrock * non-native property. 4191e9dbad6fSeschrock */ 4192e9dbad6fSeschrock start = plp; 4193e9dbad6fSeschrock while (*start != NULL) { 4194990b4856Slling if ((*start)->pl_prop == ZPROP_INVAL) 4195e9dbad6fSeschrock break; 4196e9dbad6fSeschrock start = &(*start)->pl_next; 4197e9dbad6fSeschrock } 4198e9dbad6fSeschrock 4199e9dbad6fSeschrock elem = NULL; 4200e9dbad6fSeschrock while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) { 4201e9dbad6fSeschrock /* 4202e9dbad6fSeschrock * See if we've already found this property in our list. 4203e9dbad6fSeschrock */ 4204e9dbad6fSeschrock for (last = start; *last != NULL; 4205e9dbad6fSeschrock last = &(*last)->pl_next) { 4206e9dbad6fSeschrock if (strcmp((*last)->pl_user_prop, 4207e9dbad6fSeschrock nvpair_name(elem)) == 0) 4208e9dbad6fSeschrock break; 4209e9dbad6fSeschrock } 4210e9dbad6fSeschrock 4211e9dbad6fSeschrock if (*last == NULL) { 4212e9dbad6fSeschrock if ((entry = zfs_alloc(hdl, 4213990b4856Slling sizeof (zprop_list_t))) == NULL || 4214e9dbad6fSeschrock ((entry->pl_user_prop = zfs_strdup(hdl, 4215e9dbad6fSeschrock nvpair_name(elem)))) == NULL) { 4216e9dbad6fSeschrock free(entry); 4217e9dbad6fSeschrock return (-1); 4218e9dbad6fSeschrock } 4219e9dbad6fSeschrock 4220990b4856Slling entry->pl_prop = ZPROP_INVAL; 4221e9dbad6fSeschrock entry->pl_width = strlen(nvpair_name(elem)); 4222e9dbad6fSeschrock entry->pl_all = B_TRUE; 4223e9dbad6fSeschrock *last = entry; 4224e9dbad6fSeschrock } 4225e9dbad6fSeschrock } 4226e9dbad6fSeschrock } 4227e9dbad6fSeschrock 4228e9dbad6fSeschrock /* 4229e9dbad6fSeschrock * Now go through and check the width of any non-fixed columns 4230e9dbad6fSeschrock */ 4231e9dbad6fSeschrock for (entry = *plp; entry != NULL; entry = entry->pl_next) { 423243d68d68SYuri Pankov if (entry->pl_fixed && !literal) 4233e9dbad6fSeschrock continue; 4234e9dbad6fSeschrock 4235990b4856Slling if (entry->pl_prop != ZPROP_INVAL) { 4236e9dbad6fSeschrock if (zfs_prop_get(zhp, entry->pl_prop, 423743d68d68SYuri Pankov buf, sizeof (buf), NULL, NULL, 0, literal) == 0) { 4238e9dbad6fSeschrock if (strlen(buf) > entry->pl_width) 4239e9dbad6fSeschrock entry->pl_width = strlen(buf); 4240e9dbad6fSeschrock } 424192241e0bSTom Erickson if (received && zfs_prop_get_recvd(zhp, 424292241e0bSTom Erickson zfs_prop_to_name(entry->pl_prop), 424343d68d68SYuri Pankov buf, sizeof (buf), literal) == 0) 424492241e0bSTom Erickson if (strlen(buf) > entry->pl_recvd_width) 424592241e0bSTom Erickson entry->pl_recvd_width = strlen(buf); 424692241e0bSTom Erickson } else { 424792241e0bSTom Erickson if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop, 424892241e0bSTom Erickson &propval) == 0) { 4249e9dbad6fSeschrock verify(nvlist_lookup_string(propval, 4250990b4856Slling ZPROP_VALUE, &strval) == 0); 4251e9dbad6fSeschrock if (strlen(strval) > entry->pl_width) 4252e9dbad6fSeschrock entry->pl_width = strlen(strval); 4253e9dbad6fSeschrock } 425492241e0bSTom Erickson if (received && zfs_prop_get_recvd(zhp, 425592241e0bSTom Erickson entry->pl_user_prop, 425643d68d68SYuri Pankov buf, sizeof (buf), literal) == 0) 425792241e0bSTom Erickson if (strlen(buf) > entry->pl_recvd_width) 425892241e0bSTom Erickson entry->pl_recvd_width = strlen(buf); 425992241e0bSTom Erickson } 4260e9dbad6fSeschrock } 4261e9dbad6fSeschrock 4262e9dbad6fSeschrock return (0); 4263e9dbad6fSeschrock } 4264ecd6cf80Smarks 4265ecd6cf80Smarks int 4266ecd6cf80Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path, 4267743a77edSAlan Wright char *resource, void *export, void *sharetab, 4268743a77edSAlan Wright int sharemax, zfs_share_op_t operation) 4269ecd6cf80Smarks { 4270ecd6cf80Smarks zfs_cmd_t zc = { 0 }; 4271ecd6cf80Smarks int error; 4272ecd6cf80Smarks 4273ecd6cf80Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 4274ecd6cf80Smarks (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value)); 4275743a77edSAlan Wright if (resource) 4276743a77edSAlan Wright (void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string)); 4277ecd6cf80Smarks zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab; 4278ecd6cf80Smarks zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export; 4279da6c28aaSamw zc.zc_share.z_sharetype = operation; 4280ecd6cf80Smarks zc.zc_share.z_sharemax = sharemax; 4281ecd6cf80Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc); 4282ecd6cf80Smarks return (error); 4283ecd6cf80Smarks } 42842e5e9e19SSanjeev Bagewadi 42852e5e9e19SSanjeev Bagewadi void 42862e5e9e19SSanjeev Bagewadi zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props) 42872e5e9e19SSanjeev Bagewadi { 42882e5e9e19SSanjeev Bagewadi nvpair_t *curr; 42892e5e9e19SSanjeev Bagewadi 42902e5e9e19SSanjeev Bagewadi /* 42912e5e9e19SSanjeev Bagewadi * Keep a reference to the props-table against which we prune the 42922e5e9e19SSanjeev Bagewadi * properties. 42932e5e9e19SSanjeev Bagewadi */ 42942e5e9e19SSanjeev Bagewadi zhp->zfs_props_table = props; 42952e5e9e19SSanjeev Bagewadi 42962e5e9e19SSanjeev Bagewadi curr = nvlist_next_nvpair(zhp->zfs_props, NULL); 42972e5e9e19SSanjeev Bagewadi 42982e5e9e19SSanjeev Bagewadi while (curr) { 42992e5e9e19SSanjeev Bagewadi zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr)); 43002e5e9e19SSanjeev Bagewadi nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr); 43012e5e9e19SSanjeev Bagewadi 430214843421SMatthew Ahrens /* 4303faaa6415SEric Schrock * User properties will result in ZPROP_INVAL, and since we 4304faaa6415SEric Schrock * only know how to prune standard ZFS properties, we always 4305faaa6415SEric Schrock * leave these in the list. This can also happen if we 4306faaa6415SEric Schrock * encounter an unknown DSL property (when running older 4307faaa6415SEric Schrock * software, for example). 430814843421SMatthew Ahrens */ 430914843421SMatthew Ahrens if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE) 43102e5e9e19SSanjeev Bagewadi (void) nvlist_remove(zhp->zfs_props, 43112e5e9e19SSanjeev Bagewadi nvpair_name(curr), nvpair_type(curr)); 43122e5e9e19SSanjeev Bagewadi curr = next; 43132e5e9e19SSanjeev Bagewadi } 43142e5e9e19SSanjeev Bagewadi } 4315743a77edSAlan Wright 4316743a77edSAlan Wright static int 4317743a77edSAlan Wright zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path, 4318743a77edSAlan Wright zfs_smb_acl_op_t cmd, char *resource1, char *resource2) 4319743a77edSAlan Wright { 4320743a77edSAlan Wright zfs_cmd_t zc = { 0 }; 4321743a77edSAlan Wright nvlist_t *nvlist = NULL; 4322743a77edSAlan Wright int error; 4323743a77edSAlan Wright 4324743a77edSAlan Wright (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 4325743a77edSAlan Wright (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value)); 4326743a77edSAlan Wright zc.zc_cookie = (uint64_t)cmd; 4327743a77edSAlan Wright 4328743a77edSAlan Wright if (cmd == ZFS_SMB_ACL_RENAME) { 4329743a77edSAlan Wright if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) { 4330743a77edSAlan Wright (void) no_memory(hdl); 433130925561SChris Williamson return (0); 4332743a77edSAlan Wright } 4333743a77edSAlan Wright } 4334743a77edSAlan Wright 4335743a77edSAlan Wright switch (cmd) { 4336743a77edSAlan Wright case ZFS_SMB_ACL_ADD: 4337743a77edSAlan Wright case ZFS_SMB_ACL_REMOVE: 4338743a77edSAlan Wright (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string)); 4339743a77edSAlan Wright break; 4340743a77edSAlan Wright case ZFS_SMB_ACL_RENAME: 4341743a77edSAlan Wright if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC, 4342743a77edSAlan Wright resource1) != 0) { 4343743a77edSAlan Wright (void) no_memory(hdl); 4344743a77edSAlan Wright return (-1); 4345743a77edSAlan Wright } 4346743a77edSAlan Wright if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET, 4347743a77edSAlan Wright resource2) != 0) { 4348743a77edSAlan Wright (void) no_memory(hdl); 4349743a77edSAlan Wright return (-1); 4350743a77edSAlan Wright } 4351743a77edSAlan Wright if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) { 4352743a77edSAlan Wright nvlist_free(nvlist); 4353743a77edSAlan Wright return (-1); 4354743a77edSAlan Wright } 4355743a77edSAlan Wright break; 4356743a77edSAlan Wright case ZFS_SMB_ACL_PURGE: 4357743a77edSAlan Wright break; 4358743a77edSAlan Wright default: 4359743a77edSAlan Wright return (-1); 4360743a77edSAlan Wright } 4361743a77edSAlan Wright error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc); 4362743a77edSAlan Wright nvlist_free(nvlist); 4363743a77edSAlan Wright return (error); 4364743a77edSAlan Wright } 4365743a77edSAlan Wright 4366743a77edSAlan Wright int 4367743a77edSAlan Wright zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset, 4368743a77edSAlan Wright char *path, char *resource) 4369743a77edSAlan Wright { 4370743a77edSAlan Wright return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD, 4371743a77edSAlan Wright resource, NULL)); 4372743a77edSAlan Wright } 4373743a77edSAlan Wright 4374743a77edSAlan Wright int 4375743a77edSAlan Wright zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset, 4376743a77edSAlan Wright char *path, char *resource) 4377743a77edSAlan Wright { 4378743a77edSAlan Wright return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE, 4379743a77edSAlan Wright resource, NULL)); 4380743a77edSAlan Wright } 4381743a77edSAlan Wright 4382743a77edSAlan Wright int 4383743a77edSAlan Wright zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path) 4384743a77edSAlan Wright { 4385743a77edSAlan Wright return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE, 4386743a77edSAlan Wright NULL, NULL)); 4387743a77edSAlan Wright } 4388743a77edSAlan Wright 4389743a77edSAlan Wright int 4390743a77edSAlan Wright zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path, 4391743a77edSAlan Wright char *oldname, char *newname) 4392743a77edSAlan Wright { 4393743a77edSAlan Wright return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME, 4394743a77edSAlan Wright oldname, newname)); 4395743a77edSAlan Wright } 439614843421SMatthew Ahrens 439714843421SMatthew Ahrens int 439814843421SMatthew Ahrens zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type, 439914843421SMatthew Ahrens zfs_userspace_cb_t func, void *arg) 440014843421SMatthew Ahrens { 440114843421SMatthew Ahrens zfs_cmd_t zc = { 0 }; 440214843421SMatthew Ahrens zfs_useracct_t buf[100]; 440370f56fa6SYuri Pankov libzfs_handle_t *hdl = zhp->zfs_hdl; 440470f56fa6SYuri Pankov int ret; 440514843421SMatthew Ahrens 440619b94df9SMatthew Ahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 440714843421SMatthew Ahrens 440814843421SMatthew Ahrens zc.zc_objset_type = type; 440914843421SMatthew Ahrens zc.zc_nvlist_dst = (uintptr_t)buf; 441014843421SMatthew Ahrens 441170f56fa6SYuri Pankov for (;;) { 441214843421SMatthew Ahrens zfs_useracct_t *zua = buf; 441314843421SMatthew Ahrens 441414843421SMatthew Ahrens zc.zc_nvlist_dst_size = sizeof (buf); 441570f56fa6SYuri Pankov if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) { 44163b2aab18SMatthew Ahrens char errbuf[1024]; 441770f56fa6SYuri Pankov 441870f56fa6SYuri Pankov (void) snprintf(errbuf, sizeof (errbuf), 441970f56fa6SYuri Pankov dgettext(TEXT_DOMAIN, 442070f56fa6SYuri Pankov "cannot get used/quota for %s"), zc.zc_name); 442170f56fa6SYuri Pankov return (zfs_standard_error_fmt(hdl, errno, errbuf)); 442270f56fa6SYuri Pankov } 442370f56fa6SYuri Pankov if (zc.zc_nvlist_dst_size == 0) 442414843421SMatthew Ahrens break; 442514843421SMatthew Ahrens 442614843421SMatthew Ahrens while (zc.zc_nvlist_dst_size > 0) { 442770f56fa6SYuri Pankov if ((ret = func(arg, zua->zu_domain, zua->zu_rid, 442870f56fa6SYuri Pankov zua->zu_space)) != 0) 442970f56fa6SYuri Pankov return (ret); 443014843421SMatthew Ahrens zua++; 443114843421SMatthew Ahrens zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t); 443214843421SMatthew Ahrens } 443314843421SMatthew Ahrens } 443414843421SMatthew Ahrens 443570f56fa6SYuri Pankov return (0); 443614843421SMatthew Ahrens } 4437842727c2SChris Kirby 44383b2aab18SMatthew Ahrens struct holdarg { 44393b2aab18SMatthew Ahrens nvlist_t *nvl; 44403b2aab18SMatthew Ahrens const char *snapname; 44413b2aab18SMatthew Ahrens const char *tag; 44423b2aab18SMatthew Ahrens boolean_t recursive; 4443bb6e7075SMatthew Ahrens int error; 44443b2aab18SMatthew Ahrens }; 44453b2aab18SMatthew Ahrens 44463b2aab18SMatthew Ahrens static int 44473b2aab18SMatthew Ahrens zfs_hold_one(zfs_handle_t *zhp, void *arg) 44483b2aab18SMatthew Ahrens { 44493b2aab18SMatthew Ahrens struct holdarg *ha = arg; 44509adfa60dSMatthew Ahrens char name[ZFS_MAX_DATASET_NAME_LEN]; 44513b2aab18SMatthew Ahrens int rv = 0; 44523b2aab18SMatthew Ahrens 44533b2aab18SMatthew Ahrens (void) snprintf(name, sizeof (name), 44543b2aab18SMatthew Ahrens "%s@%s", zhp->zfs_name, ha->snapname); 44553b2aab18SMatthew Ahrens 4456a7a845e4SSteven Hartland if (lzc_exists(name)) 44573b2aab18SMatthew Ahrens fnvlist_add_string(ha->nvl, name, ha->tag); 44583b2aab18SMatthew Ahrens 44593b2aab18SMatthew Ahrens if (ha->recursive) 44603b2aab18SMatthew Ahrens rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha); 44613b2aab18SMatthew Ahrens zfs_close(zhp); 44623b2aab18SMatthew Ahrens return (rv); 44633b2aab18SMatthew Ahrens } 44643b2aab18SMatthew Ahrens 4465842727c2SChris Kirby int 4466842727c2SChris Kirby zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag, 4467a7a845e4SSteven Hartland boolean_t recursive, int cleanup_fd) 4468842727c2SChris Kirby { 44693b2aab18SMatthew Ahrens int ret; 44703b2aab18SMatthew Ahrens struct holdarg ha; 4471842727c2SChris Kirby 44723b2aab18SMatthew Ahrens ha.nvl = fnvlist_alloc(); 44733b2aab18SMatthew Ahrens ha.snapname = snapname; 44743b2aab18SMatthew Ahrens ha.tag = tag; 44753b2aab18SMatthew Ahrens ha.recursive = recursive; 44763b2aab18SMatthew Ahrens (void) zfs_hold_one(zfs_handle_dup(zhp), &ha); 4477013023d4SMartin Matuska 4478a7a845e4SSteven Hartland if (nvlist_empty(ha.nvl)) { 4479a7a845e4SSteven Hartland char errbuf[1024]; 4480a7a845e4SSteven Hartland 4481013023d4SMartin Matuska fnvlist_free(ha.nvl); 4482013023d4SMartin Matuska ret = ENOENT; 4483013023d4SMartin Matuska (void) snprintf(errbuf, sizeof (errbuf), 4484013023d4SMartin Matuska dgettext(TEXT_DOMAIN, 4485013023d4SMartin Matuska "cannot hold snapshot '%s@%s'"), 4486013023d4SMartin Matuska zhp->zfs_name, snapname); 4487a7a845e4SSteven Hartland (void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf); 4488013023d4SMartin Matuska return (ret); 4489013023d4SMartin Matuska } 4490013023d4SMartin Matuska 4491a7a845e4SSteven Hartland ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl); 44923b2aab18SMatthew Ahrens fnvlist_free(ha.nvl); 4493a7f53a56SChris Kirby 4494a7a845e4SSteven Hartland return (ret); 4495a7a845e4SSteven Hartland } 4496842727c2SChris Kirby 4497a7a845e4SSteven Hartland int 4498a7a845e4SSteven Hartland zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds) 4499a7a845e4SSteven Hartland { 4500a7a845e4SSteven Hartland int ret; 4501a7a845e4SSteven Hartland nvlist_t *errors; 4502a7a845e4SSteven Hartland libzfs_handle_t *hdl = zhp->zfs_hdl; 4503a7a845e4SSteven Hartland char errbuf[1024]; 4504a7a845e4SSteven Hartland nvpair_t *elem; 4505a7a845e4SSteven Hartland 4506a7a845e4SSteven Hartland errors = NULL; 4507a7a845e4SSteven Hartland ret = lzc_hold(holds, cleanup_fd, &errors); 4508a7a845e4SSteven Hartland 4509a7a845e4SSteven Hartland if (ret == 0) { 4510a7a845e4SSteven Hartland /* There may be errors even in the success case. */ 4511a7a845e4SSteven Hartland fnvlist_free(errors); 4512a7a845e4SSteven Hartland return (0); 4513a7a845e4SSteven Hartland } 4514a7a845e4SSteven Hartland 4515a7a845e4SSteven Hartland if (nvlist_empty(errors)) { 45163b2aab18SMatthew Ahrens /* no hold-specific errors */ 45173b2aab18SMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), 45183b2aab18SMatthew Ahrens dgettext(TEXT_DOMAIN, "cannot hold")); 45193b2aab18SMatthew Ahrens switch (ret) { 45203b2aab18SMatthew Ahrens case ENOTSUP: 45213b2aab18SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 45223b2aab18SMatthew Ahrens "pool must be upgraded")); 45233b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 45243b2aab18SMatthew Ahrens break; 45253b2aab18SMatthew Ahrens case EINVAL: 45263b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 45273b2aab18SMatthew Ahrens break; 45283b2aab18SMatthew Ahrens default: 45293b2aab18SMatthew Ahrens (void) zfs_standard_error(hdl, ret, errbuf); 45303b2aab18SMatthew Ahrens } 45313b2aab18SMatthew Ahrens } 4532842727c2SChris Kirby 45333b2aab18SMatthew Ahrens for (elem = nvlist_next_nvpair(errors, NULL); 45343b2aab18SMatthew Ahrens elem != NULL; 45353b2aab18SMatthew Ahrens elem = nvlist_next_nvpair(errors, elem)) { 45363b2aab18SMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), 45373b2aab18SMatthew Ahrens dgettext(TEXT_DOMAIN, 45383b2aab18SMatthew Ahrens "cannot hold snapshot '%s'"), nvpair_name(elem)); 45393b2aab18SMatthew Ahrens switch (fnvpair_value_int32(elem)) { 454015508ac0SChris Kirby case E2BIG: 454115508ac0SChris Kirby /* 454215508ac0SChris Kirby * Temporary tags wind up having the ds object id 454315508ac0SChris Kirby * prepended. So even if we passed the length check 454415508ac0SChris Kirby * above, it's still possible for the tag to wind 454515508ac0SChris Kirby * up being slightly too long. 454615508ac0SChris Kirby */ 45473b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf); 45483b2aab18SMatthew Ahrens break; 4549842727c2SChris Kirby case EINVAL: 45503b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 45513b2aab18SMatthew Ahrens break; 4552842727c2SChris Kirby case EEXIST: 45533b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf); 45543b2aab18SMatthew Ahrens break; 4555842727c2SChris Kirby default: 45563b2aab18SMatthew Ahrens (void) zfs_standard_error(hdl, 45573b2aab18SMatthew Ahrens fnvpair_value_int32(elem), errbuf); 4558842727c2SChris Kirby } 4559842727c2SChris Kirby } 4560842727c2SChris Kirby 45613b2aab18SMatthew Ahrens fnvlist_free(errors); 45623b2aab18SMatthew Ahrens return (ret); 45633b2aab18SMatthew Ahrens } 45643b2aab18SMatthew Ahrens 45653b2aab18SMatthew Ahrens static int 45663b2aab18SMatthew Ahrens zfs_release_one(zfs_handle_t *zhp, void *arg) 45673b2aab18SMatthew Ahrens { 45683b2aab18SMatthew Ahrens struct holdarg *ha = arg; 45699adfa60dSMatthew Ahrens char name[ZFS_MAX_DATASET_NAME_LEN]; 45703b2aab18SMatthew Ahrens int rv = 0; 4571bb6e7075SMatthew Ahrens nvlist_t *existing_holds; 45723b2aab18SMatthew Ahrens 45733b2aab18SMatthew Ahrens (void) snprintf(name, sizeof (name), 45743b2aab18SMatthew Ahrens "%s@%s", zhp->zfs_name, ha->snapname); 45753b2aab18SMatthew Ahrens 4576bb6e7075SMatthew Ahrens if (lzc_get_holds(name, &existing_holds) != 0) { 4577bb6e7075SMatthew Ahrens ha->error = ENOENT; 4578bb6e7075SMatthew Ahrens } else if (!nvlist_exists(existing_holds, ha->tag)) { 4579bb6e7075SMatthew Ahrens ha->error = ESRCH; 4580bb6e7075SMatthew Ahrens } else { 4581bb6e7075SMatthew Ahrens nvlist_t *torelease = fnvlist_alloc(); 4582bb6e7075SMatthew Ahrens fnvlist_add_boolean(torelease, ha->tag); 4583bb6e7075SMatthew Ahrens fnvlist_add_nvlist(ha->nvl, name, torelease); 4584bb6e7075SMatthew Ahrens fnvlist_free(torelease); 45853b2aab18SMatthew Ahrens } 45863b2aab18SMatthew Ahrens 45873b2aab18SMatthew Ahrens if (ha->recursive) 45883b2aab18SMatthew Ahrens rv = zfs_iter_filesystems(zhp, zfs_release_one, ha); 45893b2aab18SMatthew Ahrens zfs_close(zhp); 45903b2aab18SMatthew Ahrens return (rv); 4591842727c2SChris Kirby } 4592842727c2SChris Kirby 4593842727c2SChris Kirby int 4594842727c2SChris Kirby zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag, 4595842727c2SChris Kirby boolean_t recursive) 4596842727c2SChris Kirby { 45973b2aab18SMatthew Ahrens int ret; 45983b2aab18SMatthew Ahrens struct holdarg ha; 4599a7a845e4SSteven Hartland nvlist_t *errors = NULL; 46003b2aab18SMatthew Ahrens nvpair_t *elem; 4601842727c2SChris Kirby libzfs_handle_t *hdl = zhp->zfs_hdl; 4602013023d4SMartin Matuska char errbuf[1024]; 4603842727c2SChris Kirby 46043b2aab18SMatthew Ahrens ha.nvl = fnvlist_alloc(); 46053b2aab18SMatthew Ahrens ha.snapname = snapname; 46063b2aab18SMatthew Ahrens ha.tag = tag; 46073b2aab18SMatthew Ahrens ha.recursive = recursive; 4608bb6e7075SMatthew Ahrens ha.error = 0; 46093b2aab18SMatthew Ahrens (void) zfs_release_one(zfs_handle_dup(zhp), &ha); 4610013023d4SMartin Matuska 4611a7a845e4SSteven Hartland if (nvlist_empty(ha.nvl)) { 4612013023d4SMartin Matuska fnvlist_free(ha.nvl); 4613bb6e7075SMatthew Ahrens ret = ha.error; 4614013023d4SMartin Matuska (void) snprintf(errbuf, sizeof (errbuf), 4615013023d4SMartin Matuska dgettext(TEXT_DOMAIN, 4616013023d4SMartin Matuska "cannot release hold from snapshot '%s@%s'"), 4617013023d4SMartin Matuska zhp->zfs_name, snapname); 4618bb6e7075SMatthew Ahrens if (ret == ESRCH) { 4619bb6e7075SMatthew Ahrens (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf); 4620bb6e7075SMatthew Ahrens } else { 4621013023d4SMartin Matuska (void) zfs_standard_error(hdl, ret, errbuf); 4622bb6e7075SMatthew Ahrens } 4623013023d4SMartin Matuska return (ret); 4624013023d4SMartin Matuska } 4625013023d4SMartin Matuska 46263b2aab18SMatthew Ahrens ret = lzc_release(ha.nvl, &errors); 46273b2aab18SMatthew Ahrens fnvlist_free(ha.nvl); 4628842727c2SChris Kirby 4629a7a845e4SSteven Hartland if (ret == 0) { 4630a7a845e4SSteven Hartland /* There may be errors even in the success case. */ 4631a7a845e4SSteven Hartland fnvlist_free(errors); 46323b2aab18SMatthew Ahrens return (0); 4633a7a845e4SSteven Hartland } 4634842727c2SChris Kirby 4635a7a845e4SSteven Hartland if (nvlist_empty(errors)) { 46363b2aab18SMatthew Ahrens /* no hold-specific errors */ 4637842727c2SChris Kirby (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 46383b2aab18SMatthew Ahrens "cannot release")); 4639842727c2SChris Kirby switch (errno) { 4640842727c2SChris Kirby case ENOTSUP: 4641842727c2SChris Kirby zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4642842727c2SChris Kirby "pool must be upgraded")); 46433b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 46443b2aab18SMatthew Ahrens break; 4645842727c2SChris Kirby default: 46463b2aab18SMatthew Ahrens (void) zfs_standard_error_fmt(hdl, errno, errbuf); 4647842727c2SChris Kirby } 4648842727c2SChris Kirby } 4649842727c2SChris Kirby 46503b2aab18SMatthew Ahrens for (elem = nvlist_next_nvpair(errors, NULL); 46513b2aab18SMatthew Ahrens elem != NULL; 46523b2aab18SMatthew Ahrens elem = nvlist_next_nvpair(errors, elem)) { 46533b2aab18SMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), 46543b2aab18SMatthew Ahrens dgettext(TEXT_DOMAIN, 46553b2aab18SMatthew Ahrens "cannot release hold from snapshot '%s'"), 46563b2aab18SMatthew Ahrens nvpair_name(elem)); 46573b2aab18SMatthew Ahrens switch (fnvpair_value_int32(elem)) { 46583b2aab18SMatthew Ahrens case ESRCH: 46593b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf); 46603b2aab18SMatthew Ahrens break; 46613b2aab18SMatthew Ahrens case EINVAL: 46623b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 46633b2aab18SMatthew Ahrens break; 46643b2aab18SMatthew Ahrens default: 46653b2aab18SMatthew Ahrens (void) zfs_standard_error_fmt(hdl, 46663b2aab18SMatthew Ahrens fnvpair_value_int32(elem), errbuf); 46673b2aab18SMatthew Ahrens } 46683b2aab18SMatthew Ahrens } 46693b2aab18SMatthew Ahrens 46703b2aab18SMatthew Ahrens fnvlist_free(errors); 46713b2aab18SMatthew Ahrens return (ret); 4672842727c2SChris Kirby } 4673ca45db41SChris Kirby 46741af68beaSAlexander Stetsenko int 46751af68beaSAlexander Stetsenko zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl) 46761af68beaSAlexander Stetsenko { 46771af68beaSAlexander Stetsenko zfs_cmd_t zc = { 0 }; 46781af68beaSAlexander Stetsenko libzfs_handle_t *hdl = zhp->zfs_hdl; 46791af68beaSAlexander Stetsenko int nvsz = 2048; 46801af68beaSAlexander Stetsenko void *nvbuf; 46811af68beaSAlexander Stetsenko int err = 0; 46823b2aab18SMatthew Ahrens char errbuf[1024]; 46831af68beaSAlexander Stetsenko 46841af68beaSAlexander Stetsenko assert(zhp->zfs_type == ZFS_TYPE_VOLUME || 46851af68beaSAlexander Stetsenko zhp->zfs_type == ZFS_TYPE_FILESYSTEM); 46861af68beaSAlexander Stetsenko 46871af68beaSAlexander Stetsenko tryagain: 46881af68beaSAlexander Stetsenko 46891af68beaSAlexander Stetsenko nvbuf = malloc(nvsz); 46901af68beaSAlexander Stetsenko if (nvbuf == NULL) { 46911af68beaSAlexander Stetsenko err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno))); 46921af68beaSAlexander Stetsenko goto out; 46931af68beaSAlexander Stetsenko } 46941af68beaSAlexander Stetsenko 46951af68beaSAlexander Stetsenko zc.zc_nvlist_dst_size = nvsz; 46961af68beaSAlexander Stetsenko zc.zc_nvlist_dst = (uintptr_t)nvbuf; 46971af68beaSAlexander Stetsenko 46989adfa60dSMatthew Ahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 46991af68beaSAlexander Stetsenko 4700d7f601efSGeorge Wilson if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) { 47011af68beaSAlexander Stetsenko (void) snprintf(errbuf, sizeof (errbuf), 47021af68beaSAlexander Stetsenko dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"), 47031af68beaSAlexander Stetsenko zc.zc_name); 47041af68beaSAlexander Stetsenko switch (errno) { 47051af68beaSAlexander Stetsenko case ENOMEM: 47061af68beaSAlexander Stetsenko free(nvbuf); 47071af68beaSAlexander Stetsenko nvsz = zc.zc_nvlist_dst_size; 47081af68beaSAlexander Stetsenko goto tryagain; 47091af68beaSAlexander Stetsenko 47101af68beaSAlexander Stetsenko case ENOTSUP: 47111af68beaSAlexander Stetsenko zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 47121af68beaSAlexander Stetsenko "pool must be upgraded")); 47131af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADVERSION, errbuf); 47141af68beaSAlexander Stetsenko break; 47151af68beaSAlexander Stetsenko case EINVAL: 47161af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADTYPE, errbuf); 47171af68beaSAlexander Stetsenko break; 47181af68beaSAlexander Stetsenko case ENOENT: 47191af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_NOENT, errbuf); 47201af68beaSAlexander Stetsenko break; 47211af68beaSAlexander Stetsenko default: 47221af68beaSAlexander Stetsenko err = zfs_standard_error_fmt(hdl, errno, errbuf); 47231af68beaSAlexander Stetsenko break; 47241af68beaSAlexander Stetsenko } 47251af68beaSAlexander Stetsenko } else { 47261af68beaSAlexander Stetsenko /* success */ 47271af68beaSAlexander Stetsenko int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0); 47281af68beaSAlexander Stetsenko if (rc) { 47291af68beaSAlexander Stetsenko (void) snprintf(errbuf, sizeof (errbuf), dgettext( 47301af68beaSAlexander Stetsenko TEXT_DOMAIN, "cannot get permissions on '%s'"), 47311af68beaSAlexander Stetsenko zc.zc_name); 47321af68beaSAlexander Stetsenko err = zfs_standard_error_fmt(hdl, rc, errbuf); 47331af68beaSAlexander Stetsenko } 47341af68beaSAlexander Stetsenko } 47351af68beaSAlexander Stetsenko 47361af68beaSAlexander Stetsenko free(nvbuf); 47371af68beaSAlexander Stetsenko out: 47381af68beaSAlexander Stetsenko return (err); 47391af68beaSAlexander Stetsenko } 47401af68beaSAlexander Stetsenko 47411af68beaSAlexander Stetsenko int 47421af68beaSAlexander Stetsenko zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl) 47431af68beaSAlexander Stetsenko { 47441af68beaSAlexander Stetsenko zfs_cmd_t zc = { 0 }; 47451af68beaSAlexander Stetsenko libzfs_handle_t *hdl = zhp->zfs_hdl; 47461af68beaSAlexander Stetsenko char *nvbuf; 47473b2aab18SMatthew Ahrens char errbuf[1024]; 47481af68beaSAlexander Stetsenko size_t nvsz; 47491af68beaSAlexander Stetsenko int err; 47501af68beaSAlexander Stetsenko 47511af68beaSAlexander Stetsenko assert(zhp->zfs_type == ZFS_TYPE_VOLUME || 47521af68beaSAlexander Stetsenko zhp->zfs_type == ZFS_TYPE_FILESYSTEM); 47531af68beaSAlexander Stetsenko 47541af68beaSAlexander Stetsenko err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE); 47551af68beaSAlexander Stetsenko assert(err == 0); 47561af68beaSAlexander Stetsenko 47571af68beaSAlexander Stetsenko nvbuf = malloc(nvsz); 47581af68beaSAlexander Stetsenko 47591af68beaSAlexander Stetsenko err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0); 47601af68beaSAlexander Stetsenko assert(err == 0); 47611af68beaSAlexander Stetsenko 47621af68beaSAlexander Stetsenko zc.zc_nvlist_src_size = nvsz; 47631af68beaSAlexander Stetsenko zc.zc_nvlist_src = (uintptr_t)nvbuf; 47641af68beaSAlexander Stetsenko zc.zc_perm_action = un; 47651af68beaSAlexander Stetsenko 47661af68beaSAlexander Stetsenko (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 47671af68beaSAlexander Stetsenko 47681af68beaSAlexander Stetsenko if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) { 47691af68beaSAlexander Stetsenko (void) snprintf(errbuf, sizeof (errbuf), 47701af68beaSAlexander Stetsenko dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"), 47711af68beaSAlexander Stetsenko zc.zc_name); 47721af68beaSAlexander Stetsenko switch (errno) { 47731af68beaSAlexander Stetsenko case ENOTSUP: 47741af68beaSAlexander Stetsenko zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 47751af68beaSAlexander Stetsenko "pool must be upgraded")); 47761af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADVERSION, errbuf); 47771af68beaSAlexander Stetsenko break; 47781af68beaSAlexander Stetsenko case EINVAL: 47791af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADTYPE, errbuf); 47801af68beaSAlexander Stetsenko break; 47811af68beaSAlexander Stetsenko case ENOENT: 47821af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_NOENT, errbuf); 47831af68beaSAlexander Stetsenko break; 47841af68beaSAlexander Stetsenko default: 47851af68beaSAlexander Stetsenko err = zfs_standard_error_fmt(hdl, errno, errbuf); 47861af68beaSAlexander Stetsenko break; 47871af68beaSAlexander Stetsenko } 47881af68beaSAlexander Stetsenko } 47891af68beaSAlexander Stetsenko 47901af68beaSAlexander Stetsenko free(nvbuf); 47911af68beaSAlexander Stetsenko 47921af68beaSAlexander Stetsenko return (err); 47931af68beaSAlexander Stetsenko } 47941af68beaSAlexander Stetsenko 47951af68beaSAlexander Stetsenko int 47961af68beaSAlexander Stetsenko zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl) 47971af68beaSAlexander Stetsenko { 47983b2aab18SMatthew Ahrens int err; 47993b2aab18SMatthew Ahrens char errbuf[1024]; 48003b2aab18SMatthew Ahrens 48013b2aab18SMatthew Ahrens err = lzc_get_holds(zhp->zfs_name, nvl); 48023b2aab18SMatthew Ahrens 48033b2aab18SMatthew Ahrens if (err != 0) { 48041af68beaSAlexander Stetsenko libzfs_handle_t *hdl = zhp->zfs_hdl; 48051af68beaSAlexander Stetsenko 48061af68beaSAlexander Stetsenko (void) snprintf(errbuf, sizeof (errbuf), 48071af68beaSAlexander Stetsenko dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"), 48083b2aab18SMatthew Ahrens zhp->zfs_name); 48093b2aab18SMatthew Ahrens switch (err) { 48101af68beaSAlexander Stetsenko case ENOTSUP: 48111af68beaSAlexander Stetsenko zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 48121af68beaSAlexander Stetsenko "pool must be upgraded")); 48131af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADVERSION, errbuf); 48141af68beaSAlexander Stetsenko break; 48151af68beaSAlexander Stetsenko case EINVAL: 48161af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADTYPE, errbuf); 48171af68beaSAlexander Stetsenko break; 48181af68beaSAlexander Stetsenko case ENOENT: 48191af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_NOENT, errbuf); 48201af68beaSAlexander Stetsenko break; 48211af68beaSAlexander Stetsenko default: 48221af68beaSAlexander Stetsenko err = zfs_standard_error_fmt(hdl, errno, errbuf); 48231af68beaSAlexander Stetsenko break; 48241af68beaSAlexander Stetsenko } 48251af68beaSAlexander Stetsenko } 48261af68beaSAlexander Stetsenko 48271af68beaSAlexander Stetsenko return (err); 48281af68beaSAlexander Stetsenko } 48291af68beaSAlexander Stetsenko 48303e30c24aSWill Andrews /* 48313e30c24aSWill Andrews * Convert the zvol's volume size to an appropriate reservation. 48323e30c24aSWill Andrews * Note: If this routine is updated, it is necessary to update the ZFS test 48333e30c24aSWill Andrews * suite's shell version in reservation.kshlib. 48343e30c24aSWill Andrews */ 4835c1449561SEric Taylor uint64_t 4836c1449561SEric Taylor zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props) 4837c1449561SEric Taylor { 4838c1449561SEric Taylor uint64_t numdb; 4839c1449561SEric Taylor uint64_t nblocks, volblocksize; 4840c1449561SEric Taylor int ncopies; 4841c1449561SEric Taylor char *strval; 4842c1449561SEric Taylor 4843c1449561SEric Taylor if (nvlist_lookup_string(props, 4844c1449561SEric Taylor zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0) 4845c1449561SEric Taylor ncopies = atoi(strval); 4846c1449561SEric Taylor else 4847c1449561SEric Taylor ncopies = 1; 4848c1449561SEric Taylor if (nvlist_lookup_uint64(props, 4849c1449561SEric Taylor zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 4850c1449561SEric Taylor &volblocksize) != 0) 4851c1449561SEric Taylor volblocksize = ZVOL_DEFAULT_BLOCKSIZE; 4852c1449561SEric Taylor nblocks = volsize/volblocksize; 4853c1449561SEric Taylor /* start with metadnode L0-L6 */ 4854c1449561SEric Taylor numdb = 7; 4855c1449561SEric Taylor /* calculate number of indirects */ 4856c1449561SEric Taylor while (nblocks > 1) { 4857c1449561SEric Taylor nblocks += DNODES_PER_LEVEL - 1; 4858c1449561SEric Taylor nblocks /= DNODES_PER_LEVEL; 4859c1449561SEric Taylor numdb += nblocks; 4860c1449561SEric Taylor } 4861c1449561SEric Taylor numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1); 4862c1449561SEric Taylor volsize *= ncopies; 4863c1449561SEric Taylor /* 4864c1449561SEric Taylor * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't 4865c1449561SEric Taylor * compressed, but in practice they compress down to about 4866c1449561SEric Taylor * 1100 bytes 4867c1449561SEric Taylor */ 4868c1449561SEric Taylor numdb *= 1ULL << DN_MAX_INDBLKSHIFT; 4869c1449561SEric Taylor volsize += numdb; 4870c1449561SEric Taylor return (volsize); 4871c1449561SEric Taylor } 4872