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. 240dfe541eSEvan Layton */ 250dfe541eSEvan Layton 260dfe541eSEvan Layton /* 270dfe541eSEvan Layton * Copyright (c) 2013, Joyent, Inc. All rights reserved. 28ad2760acSMatthew Ahrens * Copyright (c) 2011, 2016 by Delphix. All rights reserved. 29f0f3ef5aSGarrett D'Amore * Copyright (c) 2012 DEY Storage Systems, Inc. All rights reserved. 300d8fa8f8SMartin Matuska * Copyright (c) 2011-2012 Pawel Jakub Dawidek. All rights reserved. 31013023d4SMartin Matuska * Copyright (c) 2013 Martin Matuska. All rights reserved. 32a7a845e4SSteven Hartland * Copyright (c) 2013 Steven Hartland. All rights reserved. 33c3d26abcSMatthew Ahrens * Copyright (c) 2014 Integros [integros.com] 340dfe541eSEvan Layton * Copyright 2018 Nexenta Systems, Inc. 3588f61deeSIgor Kozhukhov * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com> 36f62db44dSAndrew Stormont * Copyright 2017-2018 RackTop Systems. 37*99ea293eSMatt Fiddaman * Copyright (c) 2021 Matt Fiddaman 38fa9e4066Sahrens */ 39fa9e4066Sahrens 40fa9e4066Sahrens #include <ctype.h> 41fa9e4066Sahrens #include <errno.h> 42fa9e4066Sahrens #include <libintl.h> 43fa9e4066Sahrens #include <stdio.h> 44fa9e4066Sahrens #include <stdlib.h> 45fa9e4066Sahrens #include <strings.h> 46fa9e4066Sahrens #include <unistd.h> 473cb34c60Sahrens #include <stddef.h> 48fa9e4066Sahrens #include <zone.h> 4999653d4eSeschrock #include <fcntl.h> 50fa9e4066Sahrens #include <sys/mntent.h> 51b12a1c38Slling #include <sys/mount.h> 52ecd6cf80Smarks #include <priv.h> 53ecd6cf80Smarks #include <pwd.h> 54ecd6cf80Smarks #include <grp.h> 55ecd6cf80Smarks #include <stddef.h> 56ecd6cf80Smarks #include <ucred.h> 5714843421SMatthew Ahrens #include <idmap.h> 5814843421SMatthew Ahrens #include <aclutils.h> 593b12c289SMatthew Ahrens #include <directory.h> 60591e0e13SSebastien Roy #include <time.h> 61fa9e4066Sahrens 62c1449561SEric Taylor #include <sys/dnode.h> 63fa9e4066Sahrens #include <sys/spa.h> 64e9dbad6fSeschrock #include <sys/zap.h> 65eb633035STom Caputi #include <sys/dsl_crypt.h> 66fa9e4066Sahrens #include <libzfs.h> 67d8ab6e12SDon Brady #include <libzutil.h> 68fa9e4066Sahrens 69fa9e4066Sahrens #include "zfs_namecheck.h" 70fa9e4066Sahrens #include "zfs_prop.h" 71fa9e4066Sahrens #include "libzfs_impl.h" 72ecd6cf80Smarks #include "zfs_deleg.h" 73fa9e4066Sahrens 7414843421SMatthew Ahrens static int userquota_propname_decode(const char *propname, boolean_t zoned, 7514843421SMatthew Ahrens zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp); 76cdf5b4caSmmusante 77fa9e4066Sahrens /* 78fa9e4066Sahrens * Given a single type (not a mask of types), return the type in a human 79fa9e4066Sahrens * readable form. 80fa9e4066Sahrens */ 81fa9e4066Sahrens const char * 82fa9e4066Sahrens zfs_type_to_name(zfs_type_t type) 83fa9e4066Sahrens { 84fa9e4066Sahrens switch (type) { 85fa9e4066Sahrens case ZFS_TYPE_FILESYSTEM: 86fa9e4066Sahrens return (dgettext(TEXT_DOMAIN, "filesystem")); 87fa9e4066Sahrens case ZFS_TYPE_SNAPSHOT: 88fa9e4066Sahrens return (dgettext(TEXT_DOMAIN, "snapshot")); 89fa9e4066Sahrens case ZFS_TYPE_VOLUME: 90fa9e4066Sahrens return (dgettext(TEXT_DOMAIN, "volume")); 9188f61deeSIgor Kozhukhov case ZFS_TYPE_POOL: 9288f61deeSIgor Kozhukhov return (dgettext(TEXT_DOMAIN, "pool")); 9388f61deeSIgor Kozhukhov case ZFS_TYPE_BOOKMARK: 9488f61deeSIgor Kozhukhov return (dgettext(TEXT_DOMAIN, "bookmark")); 9588f61deeSIgor Kozhukhov default: 9688f61deeSIgor Kozhukhov assert(!"unhandled zfs_type_t"); 97fa9e4066Sahrens } 98fa9e4066Sahrens 99fa9e4066Sahrens return (NULL); 100fa9e4066Sahrens } 101fa9e4066Sahrens 102fa9e4066Sahrens /* 103fa9e4066Sahrens * Validate a ZFS path. This is used even before trying to open the dataset, to 10414843421SMatthew Ahrens * provide a more meaningful error message. We call zfs_error_aux() to 10514843421SMatthew Ahrens * explain exactly why the name was not valid. 106fa9e4066Sahrens */ 10799d5e173STim Haley int 108f18faf3fSek110237 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type, 109f18faf3fSek110237 boolean_t modifying) 110fa9e4066Sahrens { 111fa9e4066Sahrens namecheck_err_t why; 112fa9e4066Sahrens char what; 113fa9e4066Sahrens 114edb901aaSMarcel Telka if (entity_namecheck(path, &why, &what) != 0) { 11599653d4eSeschrock if (hdl != NULL) { 116fa9e4066Sahrens switch (why) { 117b81d61a6Slling case NAME_ERR_TOOLONG: 11899653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11999653d4eSeschrock "name is too long")); 120b81d61a6Slling break; 121b81d61a6Slling 122fa9e4066Sahrens case NAME_ERR_LEADING_SLASH: 12399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 12499653d4eSeschrock "leading slash in name")); 125fa9e4066Sahrens break; 126fa9e4066Sahrens 127fa9e4066Sahrens case NAME_ERR_EMPTY_COMPONENT: 12899653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 12999653d4eSeschrock "empty component in name")); 130fa9e4066Sahrens break; 131fa9e4066Sahrens 132fa9e4066Sahrens case NAME_ERR_TRAILING_SLASH: 13399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 13499653d4eSeschrock "trailing slash in name")); 135fa9e4066Sahrens break; 136fa9e4066Sahrens 137fa9e4066Sahrens case NAME_ERR_INVALCHAR: 13899653d4eSeschrock zfs_error_aux(hdl, 139fa9e4066Sahrens dgettext(TEXT_DOMAIN, "invalid character " 14099653d4eSeschrock "'%c' in name"), what); 141fa9e4066Sahrens break; 142fa9e4066Sahrens 143edb901aaSMarcel Telka case NAME_ERR_MULTIPLE_DELIMITERS: 14499653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 145edb901aaSMarcel Telka "multiple '@' and/or '#' delimiters in " 146edb901aaSMarcel Telka "name")); 147fa9e4066Sahrens break; 1485ad82045Snd150628 1495ad82045Snd150628 case NAME_ERR_NOLETTER: 1505ad82045Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1515ad82045Snd150628 "pool doesn't begin with a letter")); 1525ad82045Snd150628 break; 1535ad82045Snd150628 1545ad82045Snd150628 case NAME_ERR_RESERVED: 1555ad82045Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1565ad82045Snd150628 "name is reserved")); 1575ad82045Snd150628 break; 1585ad82045Snd150628 1595ad82045Snd150628 case NAME_ERR_DISKLIKE: 1605ad82045Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1615ad82045Snd150628 "reserved disk name")); 1625ad82045Snd150628 break; 16388f61deeSIgor Kozhukhov 16488f61deeSIgor Kozhukhov default: 16588f61deeSIgor Kozhukhov zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 16688f61deeSIgor Kozhukhov "(%d) not defined"), why); 16788f61deeSIgor Kozhukhov break; 168fa9e4066Sahrens } 169fa9e4066Sahrens } 170fa9e4066Sahrens 171fa9e4066Sahrens return (0); 172fa9e4066Sahrens } 173fa9e4066Sahrens 174fa9e4066Sahrens if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) { 17599653d4eSeschrock if (hdl != NULL) 17699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 177edb901aaSMarcel Telka "snapshot delimiter '@' is not expected here")); 178fa9e4066Sahrens return (0); 179fa9e4066Sahrens } 180fa9e4066Sahrens 1811d452cf5Sahrens if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) { 1821d452cf5Sahrens if (hdl != NULL) 1831d452cf5Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 184d7d4af51Smmusante "missing '@' delimiter in snapshot name")); 1851d452cf5Sahrens return (0); 1861d452cf5Sahrens } 1871d452cf5Sahrens 188edb901aaSMarcel Telka if (!(type & ZFS_TYPE_BOOKMARK) && strchr(path, '#') != NULL) { 189edb901aaSMarcel Telka if (hdl != NULL) 190edb901aaSMarcel Telka zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 191edb901aaSMarcel Telka "bookmark delimiter '#' is not expected here")); 192edb901aaSMarcel Telka return (0); 193edb901aaSMarcel Telka } 194edb901aaSMarcel Telka 195edb901aaSMarcel Telka if (type == ZFS_TYPE_BOOKMARK && strchr(path, '#') == NULL) { 196edb901aaSMarcel Telka if (hdl != NULL) 197edb901aaSMarcel Telka zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 198edb901aaSMarcel Telka "missing '#' delimiter in bookmark name")); 199edb901aaSMarcel Telka return (0); 200edb901aaSMarcel Telka } 201edb901aaSMarcel Telka 202f18faf3fSek110237 if (modifying && strchr(path, '%') != NULL) { 203f18faf3fSek110237 if (hdl != NULL) 204f18faf3fSek110237 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 205f18faf3fSek110237 "invalid character %c in name"), '%'); 206f18faf3fSek110237 return (0); 207f18faf3fSek110237 } 208f18faf3fSek110237 20999653d4eSeschrock return (-1); 210fa9e4066Sahrens } 211fa9e4066Sahrens 212fa9e4066Sahrens int 213fa9e4066Sahrens zfs_name_valid(const char *name, zfs_type_t type) 214fa9e4066Sahrens { 215e7cbe64fSgw25295 if (type == ZFS_TYPE_POOL) 216e7cbe64fSgw25295 return (zpool_name_valid(NULL, B_FALSE, name)); 217f18faf3fSek110237 return (zfs_validate_name(NULL, name, type, B_FALSE)); 218fa9e4066Sahrens } 219fa9e4066Sahrens 220fa9e4066Sahrens /* 221e9dbad6fSeschrock * This function takes the raw DSL properties, and filters out the user-defined 222e9dbad6fSeschrock * properties into a separate nvlist. 223e9dbad6fSeschrock */ 224fac3008cSeschrock static nvlist_t * 225fac3008cSeschrock process_user_props(zfs_handle_t *zhp, nvlist_t *props) 226e9dbad6fSeschrock { 227e9dbad6fSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 228e9dbad6fSeschrock nvpair_t *elem; 229e9dbad6fSeschrock nvlist_t *propval; 230fac3008cSeschrock nvlist_t *nvl; 231e9dbad6fSeschrock 232fac3008cSeschrock if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) { 233fac3008cSeschrock (void) no_memory(hdl); 234fac3008cSeschrock return (NULL); 235fac3008cSeschrock } 236e9dbad6fSeschrock 237e9dbad6fSeschrock elem = NULL; 238fac3008cSeschrock while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 239e9dbad6fSeschrock if (!zfs_prop_user(nvpair_name(elem))) 240e9dbad6fSeschrock continue; 241e9dbad6fSeschrock 242e9dbad6fSeschrock verify(nvpair_value_nvlist(elem, &propval) == 0); 243fac3008cSeschrock if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) { 244fac3008cSeschrock nvlist_free(nvl); 245fac3008cSeschrock (void) no_memory(hdl); 246fac3008cSeschrock return (NULL); 247fac3008cSeschrock } 248e9dbad6fSeschrock } 249e9dbad6fSeschrock 250fac3008cSeschrock return (nvl); 251e9dbad6fSeschrock } 252e9dbad6fSeschrock 25329ab75c9Srm160521 static zpool_handle_t * 25429ab75c9Srm160521 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name) 25529ab75c9Srm160521 { 25629ab75c9Srm160521 libzfs_handle_t *hdl = zhp->zfs_hdl; 25729ab75c9Srm160521 zpool_handle_t *zph; 25829ab75c9Srm160521 25929ab75c9Srm160521 if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) { 26029ab75c9Srm160521 if (hdl->libzfs_pool_handles != NULL) 26129ab75c9Srm160521 zph->zpool_next = hdl->libzfs_pool_handles; 26229ab75c9Srm160521 hdl->libzfs_pool_handles = zph; 26329ab75c9Srm160521 } 26429ab75c9Srm160521 return (zph); 26529ab75c9Srm160521 } 26629ab75c9Srm160521 26729ab75c9Srm160521 static zpool_handle_t * 26829ab75c9Srm160521 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len) 26929ab75c9Srm160521 { 27029ab75c9Srm160521 libzfs_handle_t *hdl = zhp->zfs_hdl; 27129ab75c9Srm160521 zpool_handle_t *zph = hdl->libzfs_pool_handles; 27229ab75c9Srm160521 27329ab75c9Srm160521 while ((zph != NULL) && 27429ab75c9Srm160521 (strncmp(pool_name, zpool_get_name(zph), len) != 0)) 27529ab75c9Srm160521 zph = zph->zpool_next; 27629ab75c9Srm160521 return (zph); 27729ab75c9Srm160521 } 27829ab75c9Srm160521 27929ab75c9Srm160521 /* 28029ab75c9Srm160521 * Returns a handle to the pool that contains the provided dataset. 28129ab75c9Srm160521 * If a handle to that pool already exists then that handle is returned. 28229ab75c9Srm160521 * Otherwise, a new handle is created and added to the list of handles. 28329ab75c9Srm160521 */ 28429ab75c9Srm160521 static zpool_handle_t * 28529ab75c9Srm160521 zpool_handle(zfs_handle_t *zhp) 28629ab75c9Srm160521 { 28729ab75c9Srm160521 char *pool_name; 28829ab75c9Srm160521 int len; 28929ab75c9Srm160521 zpool_handle_t *zph; 29029ab75c9Srm160521 29178f17100SMatthew Ahrens len = strcspn(zhp->zfs_name, "/@#") + 1; 29229ab75c9Srm160521 pool_name = zfs_alloc(zhp->zfs_hdl, len); 29329ab75c9Srm160521 (void) strlcpy(pool_name, zhp->zfs_name, len); 29429ab75c9Srm160521 29529ab75c9Srm160521 zph = zpool_find_handle(zhp, pool_name, len); 29629ab75c9Srm160521 if (zph == NULL) 29729ab75c9Srm160521 zph = zpool_add_handle(zhp, pool_name); 29829ab75c9Srm160521 29929ab75c9Srm160521 free(pool_name); 30029ab75c9Srm160521 return (zph); 30129ab75c9Srm160521 } 30229ab75c9Srm160521 30329ab75c9Srm160521 void 30429ab75c9Srm160521 zpool_free_handles(libzfs_handle_t *hdl) 30529ab75c9Srm160521 { 30629ab75c9Srm160521 zpool_handle_t *next, *zph = hdl->libzfs_pool_handles; 30729ab75c9Srm160521 30829ab75c9Srm160521 while (zph != NULL) { 30929ab75c9Srm160521 next = zph->zpool_next; 31029ab75c9Srm160521 zpool_close(zph); 31129ab75c9Srm160521 zph = next; 31229ab75c9Srm160521 } 31329ab75c9Srm160521 hdl->libzfs_pool_handles = NULL; 31429ab75c9Srm160521 } 31529ab75c9Srm160521 316e9dbad6fSeschrock /* 317fa9e4066Sahrens * Utility function to gather stats (objset and zpl) for the given object. 318fa9e4066Sahrens */ 319fa9e4066Sahrens static int 320ebedde84SEric Taylor get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc) 321fa9e4066Sahrens { 322e9dbad6fSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 323fa9e4066Sahrens 324ebedde84SEric Taylor (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name)); 325fa9e4066Sahrens 326ebedde84SEric Taylor while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) { 3277f7322feSeschrock if (errno == ENOMEM) { 328ebedde84SEric Taylor if (zcmd_expand_dst_nvlist(hdl, zc) != 0) { 32999653d4eSeschrock return (-1); 330e9dbad6fSeschrock } 3317f7322feSeschrock } else { 332fa9e4066Sahrens return (-1); 3337f7322feSeschrock } 3347f7322feSeschrock } 335ebedde84SEric Taylor return (0); 336fac3008cSeschrock } 337fac3008cSeschrock 33892241e0bSTom Erickson /* 33992241e0bSTom Erickson * Utility function to get the received properties of the given object. 34092241e0bSTom Erickson */ 34192241e0bSTom Erickson static int 34292241e0bSTom Erickson get_recvd_props_ioctl(zfs_handle_t *zhp) 34392241e0bSTom Erickson { 34492241e0bSTom Erickson libzfs_handle_t *hdl = zhp->zfs_hdl; 34592241e0bSTom Erickson nvlist_t *recvdprops; 34692241e0bSTom Erickson zfs_cmd_t zc = { 0 }; 34792241e0bSTom Erickson int err; 34892241e0bSTom Erickson 34992241e0bSTom Erickson if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) 35092241e0bSTom Erickson return (-1); 35192241e0bSTom Erickson 35292241e0bSTom Erickson (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 35392241e0bSTom Erickson 35492241e0bSTom Erickson while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) { 35592241e0bSTom Erickson if (errno == ENOMEM) { 35692241e0bSTom Erickson if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 35792241e0bSTom Erickson return (-1); 35892241e0bSTom Erickson } 35992241e0bSTom Erickson } else { 36092241e0bSTom Erickson zcmd_free_nvlists(&zc); 36192241e0bSTom Erickson return (-1); 36292241e0bSTom Erickson } 36392241e0bSTom Erickson } 36492241e0bSTom Erickson 36592241e0bSTom Erickson err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops); 36692241e0bSTom Erickson zcmd_free_nvlists(&zc); 36792241e0bSTom Erickson if (err != 0) 36892241e0bSTom Erickson return (-1); 36992241e0bSTom Erickson 37092241e0bSTom Erickson nvlist_free(zhp->zfs_recvd_props); 37192241e0bSTom Erickson zhp->zfs_recvd_props = recvdprops; 37292241e0bSTom Erickson 37392241e0bSTom Erickson return (0); 37492241e0bSTom Erickson } 37592241e0bSTom Erickson 376ebedde84SEric Taylor static int 377ebedde84SEric Taylor put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc) 378ebedde84SEric Taylor { 379ebedde84SEric Taylor nvlist_t *allprops, *userprops; 380ebedde84SEric Taylor 381ebedde84SEric Taylor zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */ 382ebedde84SEric Taylor 383ebedde84SEric Taylor if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) { 384ebedde84SEric Taylor return (-1); 385ebedde84SEric Taylor } 386fac3008cSeschrock 38714843421SMatthew Ahrens /* 38814843421SMatthew Ahrens * XXX Why do we store the user props separately, in addition to 38914843421SMatthew Ahrens * storing them in zfs_props? 39014843421SMatthew Ahrens */ 391fac3008cSeschrock if ((userprops = process_user_props(zhp, allprops)) == NULL) { 392fac3008cSeschrock nvlist_free(allprops); 393fac3008cSeschrock return (-1); 394fac3008cSeschrock } 395fac3008cSeschrock 39699653d4eSeschrock nvlist_free(zhp->zfs_props); 397fac3008cSeschrock nvlist_free(zhp->zfs_user_props); 39899653d4eSeschrock 399fac3008cSeschrock zhp->zfs_props = allprops; 400fac3008cSeschrock zhp->zfs_user_props = userprops; 40199653d4eSeschrock 402fa9e4066Sahrens return (0); 403fa9e4066Sahrens } 404fa9e4066Sahrens 405ebedde84SEric Taylor static int 406ebedde84SEric Taylor get_stats(zfs_handle_t *zhp) 407ebedde84SEric Taylor { 408ebedde84SEric Taylor int rc = 0; 409ebedde84SEric Taylor zfs_cmd_t zc = { 0 }; 410ebedde84SEric Taylor 411ebedde84SEric Taylor if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 412ebedde84SEric Taylor return (-1); 413ebedde84SEric Taylor if (get_stats_ioctl(zhp, &zc) != 0) 414ebedde84SEric Taylor rc = -1; 415ebedde84SEric Taylor else if (put_stats_zhdl(zhp, &zc) != 0) 416ebedde84SEric Taylor rc = -1; 417ebedde84SEric Taylor zcmd_free_nvlists(&zc); 418ebedde84SEric Taylor return (rc); 419ebedde84SEric Taylor } 420ebedde84SEric Taylor 421fa9e4066Sahrens /* 422fa9e4066Sahrens * Refresh the properties currently stored in the handle. 423fa9e4066Sahrens */ 424fa9e4066Sahrens void 425fa9e4066Sahrens zfs_refresh_properties(zfs_handle_t *zhp) 426fa9e4066Sahrens { 427fa9e4066Sahrens (void) get_stats(zhp); 428fa9e4066Sahrens } 429fa9e4066Sahrens 430fa9e4066Sahrens /* 431fa9e4066Sahrens * Makes a handle from the given dataset name. Used by zfs_open() and 432fa9e4066Sahrens * zfs_iter_* to create child handles on the fly. 433fa9e4066Sahrens */ 434ebedde84SEric Taylor static int 435ebedde84SEric Taylor make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc) 436fa9e4066Sahrens { 437503ad85cSMatthew Ahrens if (put_stats_zhdl(zhp, zc) != 0) 438ebedde84SEric Taylor return (-1); 43931fd60d3Sahrens 440fa9e4066Sahrens /* 441fa9e4066Sahrens * We've managed to open the dataset and gather statistics. Determine 442fa9e4066Sahrens * the high-level type. 443fa9e4066Sahrens */ 444a2eea2e1Sahrens if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) 445a2eea2e1Sahrens zhp->zfs_head_type = ZFS_TYPE_VOLUME; 446a2eea2e1Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) 447a2eea2e1Sahrens zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM; 448e0f1c0afSOlaf Faaland else if (zhp->zfs_dmustats.dds_type == DMU_OST_OTHER) 449e0f1c0afSOlaf Faaland return (-1); 450a2eea2e1Sahrens else 451a2eea2e1Sahrens abort(); 452a2eea2e1Sahrens 453fa9e4066Sahrens if (zhp->zfs_dmustats.dds_is_snapshot) 454fa9e4066Sahrens zhp->zfs_type = ZFS_TYPE_SNAPSHOT; 455fa9e4066Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) 456fa9e4066Sahrens zhp->zfs_type = ZFS_TYPE_VOLUME; 457fa9e4066Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) 458fa9e4066Sahrens zhp->zfs_type = ZFS_TYPE_FILESYSTEM; 459fa9e4066Sahrens else 46099653d4eSeschrock abort(); /* we should never see any other types */ 461fa9e4066Sahrens 4629fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL) 4639fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States return (-1); 4649fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 465ebedde84SEric Taylor return (0); 466ebedde84SEric Taylor } 467ebedde84SEric Taylor 468ebedde84SEric Taylor zfs_handle_t * 469ebedde84SEric Taylor make_dataset_handle(libzfs_handle_t *hdl, const char *path) 470ebedde84SEric Taylor { 471ebedde84SEric Taylor zfs_cmd_t zc = { 0 }; 472ebedde84SEric Taylor 473ebedde84SEric Taylor zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 474ebedde84SEric Taylor 475ebedde84SEric Taylor if (zhp == NULL) 476ebedde84SEric Taylor return (NULL); 477ebedde84SEric Taylor 478ebedde84SEric Taylor zhp->zfs_hdl = hdl; 479ebedde84SEric Taylor (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name)); 480ebedde84SEric Taylor if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) { 481ebedde84SEric Taylor free(zhp); 482ebedde84SEric Taylor return (NULL); 483ebedde84SEric Taylor } 484ebedde84SEric Taylor if (get_stats_ioctl(zhp, &zc) == -1) { 485ebedde84SEric Taylor zcmd_free_nvlists(&zc); 486ebedde84SEric Taylor free(zhp); 487ebedde84SEric Taylor return (NULL); 488ebedde84SEric Taylor } 489ebedde84SEric Taylor if (make_dataset_handle_common(zhp, &zc) == -1) { 490ebedde84SEric Taylor free(zhp); 491ebedde84SEric Taylor zhp = NULL; 492ebedde84SEric Taylor } 493ebedde84SEric Taylor zcmd_free_nvlists(&zc); 494ebedde84SEric Taylor return (zhp); 495ebedde84SEric Taylor } 496ebedde84SEric Taylor 49719b94df9SMatthew Ahrens zfs_handle_t * 498ebedde84SEric Taylor make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc) 499ebedde84SEric Taylor { 500ebedde84SEric Taylor zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 501ebedde84SEric Taylor 502ebedde84SEric Taylor if (zhp == NULL) 503ebedde84SEric Taylor return (NULL); 504ebedde84SEric Taylor 505ebedde84SEric Taylor zhp->zfs_hdl = hdl; 506ebedde84SEric Taylor (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name)); 507ebedde84SEric Taylor if (make_dataset_handle_common(zhp, zc) == -1) { 508ebedde84SEric Taylor free(zhp); 509ebedde84SEric Taylor return (NULL); 510ebedde84SEric Taylor } 511fa9e4066Sahrens return (zhp); 512fa9e4066Sahrens } 513fa9e4066Sahrens 51419b94df9SMatthew Ahrens zfs_handle_t * 5150d8fa8f8SMartin Matuska make_dataset_simple_handle_zc(zfs_handle_t *pzhp, zfs_cmd_t *zc) 5160d8fa8f8SMartin Matuska { 5170d8fa8f8SMartin Matuska zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 5180d8fa8f8SMartin Matuska 5190d8fa8f8SMartin Matuska if (zhp == NULL) 5200d8fa8f8SMartin Matuska return (NULL); 5210d8fa8f8SMartin Matuska 5220d8fa8f8SMartin Matuska zhp->zfs_hdl = pzhp->zfs_hdl; 5230d8fa8f8SMartin Matuska (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name)); 5240d8fa8f8SMartin Matuska zhp->zfs_head_type = pzhp->zfs_type; 5250d8fa8f8SMartin Matuska zhp->zfs_type = ZFS_TYPE_SNAPSHOT; 5260d8fa8f8SMartin Matuska zhp->zpool_hdl = zpool_handle(zhp); 5270d8fa8f8SMartin Matuska return (zhp); 5280d8fa8f8SMartin Matuska } 5290d8fa8f8SMartin Matuska 5300d8fa8f8SMartin Matuska zfs_handle_t * 53119b94df9SMatthew Ahrens zfs_handle_dup(zfs_handle_t *zhp_orig) 53219b94df9SMatthew Ahrens { 53319b94df9SMatthew Ahrens zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 53419b94df9SMatthew Ahrens 53519b94df9SMatthew Ahrens if (zhp == NULL) 53619b94df9SMatthew Ahrens return (NULL); 53719b94df9SMatthew Ahrens 53819b94df9SMatthew Ahrens zhp->zfs_hdl = zhp_orig->zfs_hdl; 53919b94df9SMatthew Ahrens zhp->zpool_hdl = zhp_orig->zpool_hdl; 54019b94df9SMatthew Ahrens (void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name, 54119b94df9SMatthew Ahrens sizeof (zhp->zfs_name)); 54219b94df9SMatthew Ahrens zhp->zfs_type = zhp_orig->zfs_type; 54319b94df9SMatthew Ahrens zhp->zfs_head_type = zhp_orig->zfs_head_type; 54419b94df9SMatthew Ahrens zhp->zfs_dmustats = zhp_orig->zfs_dmustats; 54519b94df9SMatthew Ahrens if (zhp_orig->zfs_props != NULL) { 54619b94df9SMatthew Ahrens if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_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_user_props != NULL) { 55319b94df9SMatthew Ahrens if (nvlist_dup(zhp_orig->zfs_user_props, 55419b94df9SMatthew Ahrens &zhp->zfs_user_props, 0) != 0) { 55519b94df9SMatthew Ahrens (void) no_memory(zhp->zfs_hdl); 55619b94df9SMatthew Ahrens zfs_close(zhp); 55719b94df9SMatthew Ahrens return (NULL); 55819b94df9SMatthew Ahrens } 55919b94df9SMatthew Ahrens } 56019b94df9SMatthew Ahrens if (zhp_orig->zfs_recvd_props != NULL) { 56119b94df9SMatthew Ahrens if (nvlist_dup(zhp_orig->zfs_recvd_props, 56219b94df9SMatthew Ahrens &zhp->zfs_recvd_props, 0)) { 56319b94df9SMatthew Ahrens (void) no_memory(zhp->zfs_hdl); 56419b94df9SMatthew Ahrens zfs_close(zhp); 56519b94df9SMatthew Ahrens return (NULL); 56619b94df9SMatthew Ahrens } 56719b94df9SMatthew Ahrens } 56819b94df9SMatthew Ahrens zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck; 56919b94df9SMatthew Ahrens if (zhp_orig->zfs_mntopts != NULL) { 57019b94df9SMatthew Ahrens zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl, 57119b94df9SMatthew Ahrens zhp_orig->zfs_mntopts); 57219b94df9SMatthew Ahrens } 57319b94df9SMatthew Ahrens zhp->zfs_props_table = zhp_orig->zfs_props_table; 57419b94df9SMatthew Ahrens return (zhp); 57519b94df9SMatthew Ahrens } 57619b94df9SMatthew Ahrens 57778f17100SMatthew Ahrens boolean_t 57878f17100SMatthew Ahrens zfs_bookmark_exists(const char *path) 57978f17100SMatthew Ahrens { 58078f17100SMatthew Ahrens nvlist_t *bmarks; 58178f17100SMatthew Ahrens nvlist_t *props; 5829adfa60dSMatthew Ahrens char fsname[ZFS_MAX_DATASET_NAME_LEN]; 58378f17100SMatthew Ahrens char *bmark_name; 58478f17100SMatthew Ahrens char *pound; 58578f17100SMatthew Ahrens int err; 58678f17100SMatthew Ahrens boolean_t rv; 58778f17100SMatthew Ahrens 58878f17100SMatthew Ahrens 58978f17100SMatthew Ahrens (void) strlcpy(fsname, path, sizeof (fsname)); 59078f17100SMatthew Ahrens pound = strchr(fsname, '#'); 59178f17100SMatthew Ahrens if (pound == NULL) 59278f17100SMatthew Ahrens return (B_FALSE); 59378f17100SMatthew Ahrens 59478f17100SMatthew Ahrens *pound = '\0'; 59578f17100SMatthew Ahrens bmark_name = pound + 1; 59678f17100SMatthew Ahrens props = fnvlist_alloc(); 59778f17100SMatthew Ahrens err = lzc_get_bookmarks(fsname, props, &bmarks); 59878f17100SMatthew Ahrens nvlist_free(props); 59978f17100SMatthew Ahrens if (err != 0) { 60078f17100SMatthew Ahrens nvlist_free(bmarks); 60178f17100SMatthew Ahrens return (B_FALSE); 60278f17100SMatthew Ahrens } 60378f17100SMatthew Ahrens 60478f17100SMatthew Ahrens rv = nvlist_exists(bmarks, bmark_name); 60578f17100SMatthew Ahrens nvlist_free(bmarks); 60678f17100SMatthew Ahrens return (rv); 60778f17100SMatthew Ahrens } 60878f17100SMatthew Ahrens 60978f17100SMatthew Ahrens zfs_handle_t * 61078f17100SMatthew Ahrens make_bookmark_handle(zfs_handle_t *parent, const char *path, 61178f17100SMatthew Ahrens nvlist_t *bmark_props) 61278f17100SMatthew Ahrens { 61378f17100SMatthew Ahrens zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 61478f17100SMatthew Ahrens 61578f17100SMatthew Ahrens if (zhp == NULL) 61678f17100SMatthew Ahrens return (NULL); 61778f17100SMatthew Ahrens 61878f17100SMatthew Ahrens /* Fill in the name. */ 61978f17100SMatthew Ahrens zhp->zfs_hdl = parent->zfs_hdl; 62078f17100SMatthew Ahrens (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name)); 62178f17100SMatthew Ahrens 62278f17100SMatthew Ahrens /* Set the property lists. */ 62378f17100SMatthew Ahrens if (nvlist_dup(bmark_props, &zhp->zfs_props, 0) != 0) { 62478f17100SMatthew Ahrens free(zhp); 62578f17100SMatthew Ahrens return (NULL); 62678f17100SMatthew Ahrens } 62778f17100SMatthew Ahrens 62878f17100SMatthew Ahrens /* Set the types. */ 62978f17100SMatthew Ahrens zhp->zfs_head_type = parent->zfs_head_type; 63078f17100SMatthew Ahrens zhp->zfs_type = ZFS_TYPE_BOOKMARK; 63178f17100SMatthew Ahrens 63278f17100SMatthew Ahrens if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL) { 63378f17100SMatthew Ahrens nvlist_free(zhp->zfs_props); 63478f17100SMatthew Ahrens free(zhp); 63578f17100SMatthew Ahrens return (NULL); 63678f17100SMatthew Ahrens } 63778f17100SMatthew Ahrens 63878f17100SMatthew Ahrens return (zhp); 63978f17100SMatthew Ahrens } 64078f17100SMatthew Ahrens 641edb901aaSMarcel Telka struct zfs_open_bookmarks_cb_data { 642edb901aaSMarcel Telka const char *path; 643edb901aaSMarcel Telka zfs_handle_t *zhp; 644edb901aaSMarcel Telka }; 645edb901aaSMarcel Telka 646edb901aaSMarcel Telka static int 647edb901aaSMarcel Telka zfs_open_bookmarks_cb(zfs_handle_t *zhp, void *data) 648edb901aaSMarcel Telka { 649edb901aaSMarcel Telka struct zfs_open_bookmarks_cb_data *dp = data; 650edb901aaSMarcel Telka 651fa9e4066Sahrens /* 652edb901aaSMarcel Telka * Is it the one we are looking for? 653edb901aaSMarcel Telka */ 654edb901aaSMarcel Telka if (strcmp(dp->path, zfs_get_name(zhp)) == 0) { 655edb901aaSMarcel Telka /* 656edb901aaSMarcel Telka * We found it. Save it and let the caller know we are done. 657edb901aaSMarcel Telka */ 658edb901aaSMarcel Telka dp->zhp = zhp; 659edb901aaSMarcel Telka return (EEXIST); 660edb901aaSMarcel Telka } 661edb901aaSMarcel Telka 662edb901aaSMarcel Telka /* 663edb901aaSMarcel Telka * Not found. Close the handle and ask for another one. 664edb901aaSMarcel Telka */ 665edb901aaSMarcel Telka zfs_close(zhp); 666edb901aaSMarcel Telka return (0); 667edb901aaSMarcel Telka } 668edb901aaSMarcel Telka 669edb901aaSMarcel Telka /* 670edb901aaSMarcel Telka * Opens the given snapshot, bookmark, filesystem, or volume. The 'types' 671fa9e4066Sahrens * argument is a mask of acceptable types. The function will print an 672fa9e4066Sahrens * appropriate error message and return NULL if it can't be opened. 673fa9e4066Sahrens */ 674fa9e4066Sahrens zfs_handle_t * 67599653d4eSeschrock zfs_open(libzfs_handle_t *hdl, const char *path, int types) 676fa9e4066Sahrens { 677fa9e4066Sahrens zfs_handle_t *zhp; 67899653d4eSeschrock char errbuf[1024]; 679edb901aaSMarcel Telka char *bookp; 68099653d4eSeschrock 68199653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), 68299653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), path); 683fa9e4066Sahrens 684fa9e4066Sahrens /* 68599653d4eSeschrock * Validate the name before we even try to open it. 686fa9e4066Sahrens */ 687edb901aaSMarcel Telka if (!zfs_validate_name(hdl, path, types, B_FALSE)) { 68899653d4eSeschrock (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 689fa9e4066Sahrens return (NULL); 690fa9e4066Sahrens } 691fa9e4066Sahrens 692fa9e4066Sahrens /* 693edb901aaSMarcel Telka * Bookmarks needs to be handled separately. 694edb901aaSMarcel Telka */ 695edb901aaSMarcel Telka bookp = strchr(path, '#'); 696edb901aaSMarcel Telka if (bookp == NULL) { 697edb901aaSMarcel Telka /* 698edb901aaSMarcel Telka * Try to get stats for the dataset, which will tell us if it 699edb901aaSMarcel Telka * exists. 700fa9e4066Sahrens */ 701fa9e4066Sahrens errno = 0; 70299653d4eSeschrock if ((zhp = make_dataset_handle(hdl, path)) == NULL) { 703ece3d9b3Slling (void) zfs_standard_error(hdl, errno, errbuf); 704fa9e4066Sahrens return (NULL); 705fa9e4066Sahrens } 706edb901aaSMarcel Telka } else { 707edb901aaSMarcel Telka char dsname[ZFS_MAX_DATASET_NAME_LEN]; 708edb901aaSMarcel Telka zfs_handle_t *pzhp; 709edb901aaSMarcel Telka struct zfs_open_bookmarks_cb_data cb_data = {path, NULL}; 710edb901aaSMarcel Telka 711edb901aaSMarcel Telka /* 712edb901aaSMarcel Telka * We need to cut out '#' and everything after '#' 713edb901aaSMarcel Telka * to get the parent dataset name only. 714edb901aaSMarcel Telka */ 715edb901aaSMarcel Telka assert(bookp - path < sizeof (dsname)); 716edb901aaSMarcel Telka (void) strncpy(dsname, path, bookp - path); 717edb901aaSMarcel Telka dsname[bookp - path] = '\0'; 718edb901aaSMarcel Telka 719edb901aaSMarcel Telka /* 720edb901aaSMarcel Telka * Create handle for the parent dataset. 721edb901aaSMarcel Telka */ 722edb901aaSMarcel Telka errno = 0; 723edb901aaSMarcel Telka if ((pzhp = make_dataset_handle(hdl, dsname)) == NULL) { 724edb901aaSMarcel Telka (void) zfs_standard_error(hdl, errno, errbuf); 725edb901aaSMarcel Telka return (NULL); 726edb901aaSMarcel Telka } 727edb901aaSMarcel Telka 728edb901aaSMarcel Telka /* 729edb901aaSMarcel Telka * Iterate bookmarks to find the right one. 730edb901aaSMarcel Telka */ 731edb901aaSMarcel Telka errno = 0; 732edb901aaSMarcel Telka if ((zfs_iter_bookmarks(pzhp, zfs_open_bookmarks_cb, 733edb901aaSMarcel Telka &cb_data) == 0) && (cb_data.zhp == NULL)) { 734edb901aaSMarcel Telka (void) zfs_error(hdl, EZFS_NOENT, errbuf); 735edb901aaSMarcel Telka zfs_close(pzhp); 736edb901aaSMarcel Telka return (NULL); 737edb901aaSMarcel Telka } 738edb901aaSMarcel Telka if (cb_data.zhp == NULL) { 739edb901aaSMarcel Telka (void) zfs_standard_error(hdl, errno, errbuf); 740edb901aaSMarcel Telka zfs_close(pzhp); 741edb901aaSMarcel Telka return (NULL); 742edb901aaSMarcel Telka } 743edb901aaSMarcel Telka zhp = cb_data.zhp; 744edb901aaSMarcel Telka 745edb901aaSMarcel Telka /* 746edb901aaSMarcel Telka * Cleanup. 747edb901aaSMarcel Telka */ 748edb901aaSMarcel Telka zfs_close(pzhp); 749edb901aaSMarcel Telka } 750fa9e4066Sahrens 751fa9e4066Sahrens if (!(types & zhp->zfs_type)) { 75299653d4eSeschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 75394de1d4cSeschrock zfs_close(zhp); 754fa9e4066Sahrens return (NULL); 755fa9e4066Sahrens } 756fa9e4066Sahrens 757fa9e4066Sahrens return (zhp); 758fa9e4066Sahrens } 759fa9e4066Sahrens 760fa9e4066Sahrens /* 761fa9e4066Sahrens * Release a ZFS handle. Nothing to do but free the associated memory. 762fa9e4066Sahrens */ 763fa9e4066Sahrens void 764fa9e4066Sahrens zfs_close(zfs_handle_t *zhp) 765fa9e4066Sahrens { 766fa9e4066Sahrens if (zhp->zfs_mntopts) 767fa9e4066Sahrens free(zhp->zfs_mntopts); 76899653d4eSeschrock nvlist_free(zhp->zfs_props); 769e9dbad6fSeschrock nvlist_free(zhp->zfs_user_props); 77092241e0bSTom Erickson nvlist_free(zhp->zfs_recvd_props); 771fa9e4066Sahrens free(zhp); 772fa9e4066Sahrens } 773fa9e4066Sahrens 774ebedde84SEric Taylor typedef struct mnttab_node { 775ebedde84SEric Taylor struct mnttab mtn_mt; 776ebedde84SEric Taylor avl_node_t mtn_node; 777ebedde84SEric Taylor } mnttab_node_t; 778ebedde84SEric Taylor 779ebedde84SEric Taylor static int 780ebedde84SEric Taylor libzfs_mnttab_cache_compare(const void *arg1, const void *arg2) 781ebedde84SEric Taylor { 782c4ab0d3fSGvozden Neskovic const mnttab_node_t *mtn1 = (const mnttab_node_t *)arg1; 783c4ab0d3fSGvozden Neskovic const mnttab_node_t *mtn2 = (const mnttab_node_t *)arg2; 784ebedde84SEric Taylor int rv; 785ebedde84SEric Taylor 786ebedde84SEric Taylor rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special); 787ebedde84SEric Taylor 7884d7988d6SPaul Dagnelie if (rv == 0) 7894d7988d6SPaul Dagnelie return (0); 7904d7988d6SPaul Dagnelie return (rv > 0 ? 1 : -1); 791ebedde84SEric Taylor } 792ebedde84SEric Taylor 793ebedde84SEric Taylor void 794ebedde84SEric Taylor libzfs_mnttab_init(libzfs_handle_t *hdl) 795ebedde84SEric Taylor { 796591e0e13SSebastien Roy (void) mutex_init(&hdl->libzfs_mnttab_cache_lock, 797591e0e13SSebastien Roy LOCK_NORMAL | LOCK_ERRORCHECK, NULL); 798ebedde84SEric Taylor assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0); 799ebedde84SEric Taylor avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare, 800ebedde84SEric Taylor sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node)); 801b2634b9cSEric Taylor } 802b2634b9cSEric Taylor 803b2634b9cSEric Taylor void 804b2634b9cSEric Taylor libzfs_mnttab_update(libzfs_handle_t *hdl) 805b2634b9cSEric Taylor { 806b2634b9cSEric Taylor struct mnttab entry; 807ebedde84SEric Taylor 808ebedde84SEric Taylor rewind(hdl->libzfs_mnttab); 809ebedde84SEric Taylor while (getmntent(hdl->libzfs_mnttab, &entry) == 0) { 810ebedde84SEric Taylor mnttab_node_t *mtn; 811ebedde84SEric Taylor 812ebedde84SEric Taylor if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) 813ebedde84SEric Taylor continue; 814ebedde84SEric Taylor mtn = zfs_alloc(hdl, sizeof (mnttab_node_t)); 815ebedde84SEric Taylor mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special); 816ebedde84SEric Taylor mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp); 817ebedde84SEric Taylor mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype); 818ebedde84SEric Taylor mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts); 819ebedde84SEric Taylor avl_add(&hdl->libzfs_mnttab_cache, mtn); 820ebedde84SEric Taylor } 821ebedde84SEric Taylor } 822ebedde84SEric Taylor 823ebedde84SEric Taylor void 824ebedde84SEric Taylor libzfs_mnttab_fini(libzfs_handle_t *hdl) 825ebedde84SEric Taylor { 826ebedde84SEric Taylor void *cookie = NULL; 827ebedde84SEric Taylor mnttab_node_t *mtn; 828ebedde84SEric Taylor 82988f61deeSIgor Kozhukhov while ((mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie)) 83088f61deeSIgor Kozhukhov != NULL) { 831ebedde84SEric Taylor free(mtn->mtn_mt.mnt_special); 832ebedde84SEric Taylor free(mtn->mtn_mt.mnt_mountp); 833ebedde84SEric Taylor free(mtn->mtn_mt.mnt_fstype); 834ebedde84SEric Taylor free(mtn->mtn_mt.mnt_mntopts); 835ebedde84SEric Taylor free(mtn); 836ebedde84SEric Taylor } 837ebedde84SEric Taylor avl_destroy(&hdl->libzfs_mnttab_cache); 838591e0e13SSebastien Roy (void) mutex_destroy(&hdl->libzfs_mnttab_cache_lock); 839ebedde84SEric Taylor } 840ebedde84SEric Taylor 841b2634b9cSEric Taylor void 842b2634b9cSEric Taylor libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable) 843b2634b9cSEric Taylor { 844b2634b9cSEric Taylor hdl->libzfs_mnttab_enable = enable; 845b2634b9cSEric Taylor } 846b2634b9cSEric Taylor 847ebedde84SEric Taylor int 848ebedde84SEric Taylor libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname, 849ebedde84SEric Taylor struct mnttab *entry) 850ebedde84SEric Taylor { 851ebedde84SEric Taylor mnttab_node_t find; 852ebedde84SEric Taylor mnttab_node_t *mtn; 853591e0e13SSebastien Roy int ret = ENOENT; 854ebedde84SEric Taylor 855b2634b9cSEric Taylor if (!hdl->libzfs_mnttab_enable) { 856b2634b9cSEric Taylor struct mnttab srch = { 0 }; 857b2634b9cSEric Taylor 858b2634b9cSEric Taylor if (avl_numnodes(&hdl->libzfs_mnttab_cache)) 859b2634b9cSEric Taylor libzfs_mnttab_fini(hdl); 860b2634b9cSEric Taylor rewind(hdl->libzfs_mnttab); 861b2634b9cSEric Taylor srch.mnt_special = (char *)fsname; 862b2634b9cSEric Taylor srch.mnt_fstype = MNTTYPE_ZFS; 863b2634b9cSEric Taylor if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0) 864b2634b9cSEric Taylor return (0); 865b2634b9cSEric Taylor else 866b2634b9cSEric Taylor return (ENOENT); 867b2634b9cSEric Taylor } 868b2634b9cSEric Taylor 869591e0e13SSebastien Roy mutex_enter(&hdl->libzfs_mnttab_cache_lock); 870ebedde84SEric Taylor if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0) 871b2634b9cSEric Taylor libzfs_mnttab_update(hdl); 872ebedde84SEric Taylor 873ebedde84SEric Taylor find.mtn_mt.mnt_special = (char *)fsname; 874ebedde84SEric Taylor mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL); 875ebedde84SEric Taylor if (mtn) { 876ebedde84SEric Taylor *entry = mtn->mtn_mt; 877591e0e13SSebastien Roy ret = 0; 878ebedde84SEric Taylor } 879591e0e13SSebastien Roy mutex_exit(&hdl->libzfs_mnttab_cache_lock); 880591e0e13SSebastien Roy return (ret); 881ebedde84SEric Taylor } 882ebedde84SEric Taylor 883ebedde84SEric Taylor void 884ebedde84SEric Taylor libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special, 885ebedde84SEric Taylor const char *mountp, const char *mntopts) 886ebedde84SEric Taylor { 887ebedde84SEric Taylor mnttab_node_t *mtn; 888ebedde84SEric Taylor 889591e0e13SSebastien Roy mutex_enter(&hdl->libzfs_mnttab_cache_lock); 890591e0e13SSebastien Roy if (avl_numnodes(&hdl->libzfs_mnttab_cache) != 0) { 891ebedde84SEric Taylor mtn = zfs_alloc(hdl, sizeof (mnttab_node_t)); 892ebedde84SEric Taylor mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special); 893ebedde84SEric Taylor mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp); 894ebedde84SEric Taylor mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS); 895ebedde84SEric Taylor mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts); 896ebedde84SEric Taylor avl_add(&hdl->libzfs_mnttab_cache, mtn); 897ebedde84SEric Taylor } 898591e0e13SSebastien Roy mutex_exit(&hdl->libzfs_mnttab_cache_lock); 899591e0e13SSebastien Roy } 900ebedde84SEric Taylor 901ebedde84SEric Taylor void 902ebedde84SEric Taylor libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname) 903ebedde84SEric Taylor { 904ebedde84SEric Taylor mnttab_node_t find; 905ebedde84SEric Taylor mnttab_node_t *ret; 906ebedde84SEric Taylor 907591e0e13SSebastien Roy mutex_enter(&hdl->libzfs_mnttab_cache_lock); 908ebedde84SEric Taylor find.mtn_mt.mnt_special = (char *)fsname; 90988f61deeSIgor Kozhukhov if ((ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL)) 91088f61deeSIgor Kozhukhov != NULL) { 911ebedde84SEric Taylor avl_remove(&hdl->libzfs_mnttab_cache, ret); 912ebedde84SEric Taylor free(ret->mtn_mt.mnt_special); 913ebedde84SEric Taylor free(ret->mtn_mt.mnt_mountp); 914ebedde84SEric Taylor free(ret->mtn_mt.mnt_fstype); 915ebedde84SEric Taylor free(ret->mtn_mt.mnt_mntopts); 916ebedde84SEric Taylor free(ret); 917ebedde84SEric Taylor } 918591e0e13SSebastien Roy mutex_exit(&hdl->libzfs_mnttab_cache_lock); 919ebedde84SEric Taylor } 920ebedde84SEric Taylor 9217b97dc1aSrm160521 int 9227b97dc1aSrm160521 zfs_spa_version(zfs_handle_t *zhp, int *spa_version) 9237b97dc1aSrm160521 { 92429ab75c9Srm160521 zpool_handle_t *zpool_handle = zhp->zpool_hdl; 9257b97dc1aSrm160521 9267b97dc1aSrm160521 if (zpool_handle == NULL) 9277b97dc1aSrm160521 return (-1); 9287b97dc1aSrm160521 9297b97dc1aSrm160521 *spa_version = zpool_get_prop_int(zpool_handle, 9307b97dc1aSrm160521 ZPOOL_PROP_VERSION, NULL); 9317b97dc1aSrm160521 return (0); 9327b97dc1aSrm160521 } 9337b97dc1aSrm160521 9347b97dc1aSrm160521 /* 9357b97dc1aSrm160521 * The choice of reservation property depends on the SPA version. 9367b97dc1aSrm160521 */ 9377b97dc1aSrm160521 static int 9387b97dc1aSrm160521 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop) 9397b97dc1aSrm160521 { 9407b97dc1aSrm160521 int spa_version; 9417b97dc1aSrm160521 9427b97dc1aSrm160521 if (zfs_spa_version(zhp, &spa_version) < 0) 9437b97dc1aSrm160521 return (-1); 9447b97dc1aSrm160521 9457b97dc1aSrm160521 if (spa_version >= SPA_VERSION_REFRESERVATION) 9467b97dc1aSrm160521 *resv_prop = ZFS_PROP_REFRESERVATION; 9477b97dc1aSrm160521 else 9487b97dc1aSrm160521 *resv_prop = ZFS_PROP_RESERVATION; 9497b97dc1aSrm160521 9507b97dc1aSrm160521 return (0); 9517b97dc1aSrm160521 } 9527b97dc1aSrm160521 953b1b8ab34Slling /* 954e9dbad6fSeschrock * Given an nvlist of properties to set, validates that they are correct, and 955e9dbad6fSeschrock * parses any numeric properties (index, boolean, etc) if they are specified as 956e9dbad6fSeschrock * strings. 957fa9e4066Sahrens */ 9580a48a24eStimh nvlist_t * 9590a48a24eStimh zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl, 960e9316f76SJoe Stein uint64_t zoned, zfs_handle_t *zhp, zpool_handle_t *zpool_hdl, 961eb633035STom Caputi boolean_t key_params_ok, const char *errbuf) 962fa9e4066Sahrens { 963e9dbad6fSeschrock nvpair_t *elem; 964e9dbad6fSeschrock uint64_t intval; 965e9dbad6fSeschrock char *strval; 966990b4856Slling zfs_prop_t prop; 967e9dbad6fSeschrock nvlist_t *ret; 968da6c28aaSamw int chosen_normal = -1; 969da6c28aaSamw int chosen_utf = -1; 970990b4856Slling 971e9dbad6fSeschrock if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) { 972e9dbad6fSeschrock (void) no_memory(hdl); 973e9dbad6fSeschrock return (NULL); 974e9dbad6fSeschrock } 975fa9e4066Sahrens 97614843421SMatthew Ahrens /* 97714843421SMatthew Ahrens * Make sure this property is valid and applies to this type. 97814843421SMatthew Ahrens */ 97914843421SMatthew Ahrens 980e9dbad6fSeschrock elem = NULL; 981e9dbad6fSeschrock while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { 982990b4856Slling const char *propname = nvpair_name(elem); 98399653d4eSeschrock 98414843421SMatthew Ahrens prop = zfs_name_to_prop(propname); 98514843421SMatthew Ahrens if (prop == ZPROP_INVAL && zfs_prop_user(propname)) { 986fa9e4066Sahrens /* 98714843421SMatthew Ahrens * This is a user property: make sure it's a 988990b4856Slling * string, and that it's less than ZAP_MAXNAMELEN. 989990b4856Slling */ 990990b4856Slling if (nvpair_type(elem) != DATA_TYPE_STRING) { 991990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 992990b4856Slling "'%s' must be a string"), propname); 993990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 994990b4856Slling goto error; 995990b4856Slling } 996990b4856Slling 997990b4856Slling if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) { 998e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 999e9dbad6fSeschrock "property name '%s' is too long"), 1000e9dbad6fSeschrock propname); 1001990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1002e9dbad6fSeschrock goto error; 1003e9dbad6fSeschrock } 1004e9dbad6fSeschrock 1005e9dbad6fSeschrock (void) nvpair_value_string(elem, &strval); 1006e9dbad6fSeschrock if (nvlist_add_string(ret, propname, strval) != 0) { 1007e9dbad6fSeschrock (void) no_memory(hdl); 1008e9dbad6fSeschrock goto error; 1009e9dbad6fSeschrock } 1010e9dbad6fSeschrock continue; 1011e9dbad6fSeschrock } 1012fa9e4066Sahrens 101314843421SMatthew Ahrens /* 101414843421SMatthew Ahrens * Currently, only user properties can be modified on 101514843421SMatthew Ahrens * snapshots. 101614843421SMatthew Ahrens */ 1017bb0ade09Sahrens if (type == ZFS_TYPE_SNAPSHOT) { 1018bb0ade09Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1019bb0ade09Sahrens "this property can not be modified for snapshots")); 1020bb0ade09Sahrens (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 1021bb0ade09Sahrens goto error; 1022bb0ade09Sahrens } 1023bb0ade09Sahrens 102414843421SMatthew Ahrens if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) { 102514843421SMatthew Ahrens zfs_userquota_prop_t uqtype; 102614843421SMatthew Ahrens char newpropname[128]; 102714843421SMatthew Ahrens char domain[128]; 102814843421SMatthew Ahrens uint64_t rid; 102914843421SMatthew Ahrens uint64_t valary[3]; 103014843421SMatthew Ahrens 103114843421SMatthew Ahrens if (userquota_propname_decode(propname, zoned, 103214843421SMatthew Ahrens &uqtype, domain, sizeof (domain), &rid) != 0) { 103314843421SMatthew Ahrens zfs_error_aux(hdl, 103414843421SMatthew Ahrens dgettext(TEXT_DOMAIN, 103514843421SMatthew Ahrens "'%s' has an invalid user/group name"), 103614843421SMatthew Ahrens propname); 103714843421SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 103814843421SMatthew Ahrens goto error; 103914843421SMatthew Ahrens } 104014843421SMatthew Ahrens 104114843421SMatthew Ahrens if (uqtype != ZFS_PROP_USERQUOTA && 1042f67950b2SNasf-Fan uqtype != ZFS_PROP_GROUPQUOTA && 1043f67950b2SNasf-Fan uqtype != ZFS_PROP_USEROBJQUOTA && 1044f67950b2SNasf-Fan uqtype != ZFS_PROP_GROUPOBJQUOTA && 1045f67950b2SNasf-Fan uqtype != ZFS_PROP_PROJECTQUOTA && 1046f67950b2SNasf-Fan uqtype != ZFS_PROP_PROJECTOBJQUOTA) { 104714843421SMatthew Ahrens zfs_error_aux(hdl, 104814843421SMatthew Ahrens dgettext(TEXT_DOMAIN, "'%s' is readonly"), 104914843421SMatthew Ahrens propname); 105014843421SMatthew Ahrens (void) zfs_error(hdl, EZFS_PROPREADONLY, 105114843421SMatthew Ahrens errbuf); 105214843421SMatthew Ahrens goto error; 105314843421SMatthew Ahrens } 105414843421SMatthew Ahrens 105514843421SMatthew Ahrens if (nvpair_type(elem) == DATA_TYPE_STRING) { 105614843421SMatthew Ahrens (void) nvpair_value_string(elem, &strval); 105714843421SMatthew Ahrens if (strcmp(strval, "none") == 0) { 105814843421SMatthew Ahrens intval = 0; 105914843421SMatthew Ahrens } else if (zfs_nicestrtonum(hdl, 106014843421SMatthew Ahrens strval, &intval) != 0) { 106114843421SMatthew Ahrens (void) zfs_error(hdl, 106214843421SMatthew Ahrens EZFS_BADPROP, errbuf); 106314843421SMatthew Ahrens goto error; 106414843421SMatthew Ahrens } 106514843421SMatthew Ahrens } else if (nvpair_type(elem) == 106614843421SMatthew Ahrens DATA_TYPE_UINT64) { 106714843421SMatthew Ahrens (void) nvpair_value_uint64(elem, &intval); 106814843421SMatthew Ahrens if (intval == 0) { 106914843421SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 107014843421SMatthew Ahrens "use 'none' to disable " 1071f67950b2SNasf-Fan "{user|group|project}quota")); 107214843421SMatthew Ahrens goto error; 107314843421SMatthew Ahrens } 107414843421SMatthew Ahrens } else { 107514843421SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 107614843421SMatthew Ahrens "'%s' must be a number"), propname); 107714843421SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 107814843421SMatthew Ahrens goto error; 107914843421SMatthew Ahrens } 108014843421SMatthew Ahrens 10812d5843dbSMatthew Ahrens /* 10822d5843dbSMatthew Ahrens * Encode the prop name as 10832d5843dbSMatthew Ahrens * userquota@<hex-rid>-domain, to make it easy 10842d5843dbSMatthew Ahrens * for the kernel to decode. 10852d5843dbSMatthew Ahrens */ 108614843421SMatthew Ahrens (void) snprintf(newpropname, sizeof (newpropname), 10872d5843dbSMatthew Ahrens "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype], 10882d5843dbSMatthew Ahrens (longlong_t)rid, domain); 108914843421SMatthew Ahrens valary[0] = uqtype; 109014843421SMatthew Ahrens valary[1] = rid; 109114843421SMatthew Ahrens valary[2] = intval; 109214843421SMatthew Ahrens if (nvlist_add_uint64_array(ret, newpropname, 109314843421SMatthew Ahrens valary, 3) != 0) { 109414843421SMatthew Ahrens (void) no_memory(hdl); 109514843421SMatthew Ahrens goto error; 109614843421SMatthew Ahrens } 109714843421SMatthew Ahrens continue; 109819b94df9SMatthew Ahrens } else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) { 109919b94df9SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 110019b94df9SMatthew Ahrens "'%s' is readonly"), 110119b94df9SMatthew Ahrens propname); 110219b94df9SMatthew Ahrens (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 110319b94df9SMatthew Ahrens goto error; 110414843421SMatthew Ahrens } 110514843421SMatthew Ahrens 110614843421SMatthew Ahrens if (prop == ZPROP_INVAL) { 110714843421SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 110814843421SMatthew Ahrens "invalid property '%s'"), propname); 110914843421SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 111014843421SMatthew Ahrens goto error; 111114843421SMatthew Ahrens } 111214843421SMatthew Ahrens 1113e9dbad6fSeschrock if (!zfs_prop_valid_for_type(prop, type)) { 1114e9dbad6fSeschrock zfs_error_aux(hdl, 1115e9dbad6fSeschrock dgettext(TEXT_DOMAIN, "'%s' does not " 1116e9dbad6fSeschrock "apply to datasets of this type"), propname); 1117e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 1118e9dbad6fSeschrock goto error; 1119e9dbad6fSeschrock } 1120e9dbad6fSeschrock 1121e9dbad6fSeschrock if (zfs_prop_readonly(prop) && 1122eb633035STom Caputi !(zfs_prop_setonce(prop) && zhp == NULL) && 1123eb633035STom Caputi !(zfs_prop_encryption_key_param(prop) && key_params_ok)) { 1124e9dbad6fSeschrock zfs_error_aux(hdl, 1125e9dbad6fSeschrock dgettext(TEXT_DOMAIN, "'%s' is readonly"), 1126e9dbad6fSeschrock propname); 1127e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 1128e9dbad6fSeschrock goto error; 1129e9dbad6fSeschrock } 1130e9dbad6fSeschrock 1131990b4856Slling if (zprop_parse_value(hdl, elem, prop, type, ret, 1132990b4856Slling &strval, &intval, errbuf) != 0) 1133e9dbad6fSeschrock goto error; 1134e9dbad6fSeschrock 1135e9dbad6fSeschrock /* 1136e9dbad6fSeschrock * Perform some additional checks for specific properties. 1137e9dbad6fSeschrock */ 1138e9dbad6fSeschrock switch (prop) { 1139e7437265Sahrens case ZFS_PROP_VERSION: 1140e7437265Sahrens { 1141e7437265Sahrens int version; 1142e7437265Sahrens 1143e7437265Sahrens if (zhp == NULL) 1144e7437265Sahrens break; 1145e7437265Sahrens version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION); 1146e7437265Sahrens if (intval < version) { 1147e7437265Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1148e7437265Sahrens "Can not downgrade; already at version %u"), 1149e7437265Sahrens version); 1150e7437265Sahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1151e7437265Sahrens goto error; 1152e7437265Sahrens } 1153e7437265Sahrens break; 1154e7437265Sahrens } 1155e7437265Sahrens 1156e9dbad6fSeschrock case ZFS_PROP_VOLBLOCKSIZE: 1157b5152584SMatthew Ahrens case ZFS_PROP_RECORDSIZE: 1158b5152584SMatthew Ahrens { 1159b5152584SMatthew Ahrens int maxbs = SPA_MAXBLOCKSIZE; 1160e9316f76SJoe Stein if (zpool_hdl != NULL) { 1161e9316f76SJoe Stein maxbs = zpool_get_prop_int(zpool_hdl, 1162b5152584SMatthew Ahrens ZPOOL_PROP_MAXBLOCKSIZE, NULL); 1163b5152584SMatthew Ahrens } 1164b5152584SMatthew Ahrens /* 1165b5152584SMatthew Ahrens * Volumes are limited to a volblocksize of 128KB, 1166b5152584SMatthew Ahrens * because they typically service workloads with 1167b5152584SMatthew Ahrens * small random writes, which incur a large performance 1168b5152584SMatthew Ahrens * penalty with large blocks. 1169b5152584SMatthew Ahrens */ 1170b5152584SMatthew Ahrens if (prop == ZFS_PROP_VOLBLOCKSIZE) 1171b5152584SMatthew Ahrens maxbs = SPA_OLD_MAXBLOCKSIZE; 1172b5152584SMatthew Ahrens /* 1173b5152584SMatthew Ahrens * The value must be a power of two between 1174b5152584SMatthew Ahrens * SPA_MINBLOCKSIZE and maxbs. 1175b5152584SMatthew Ahrens */ 1176e9dbad6fSeschrock if (intval < SPA_MINBLOCKSIZE || 1177b5152584SMatthew Ahrens intval > maxbs || !ISP2(intval)) { 1178e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1179b5152584SMatthew Ahrens "'%s' must be power of 2 from 512B " 1180b5152584SMatthew Ahrens "to %uKB"), propname, maxbs >> 10); 1181e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1182e9dbad6fSeschrock goto error; 1183e9dbad6fSeschrock } 1184e9dbad6fSeschrock break; 1185b5152584SMatthew Ahrens } 1186663207adSDon Brady 1187663207adSDon Brady case ZFS_PROP_SPECIAL_SMALL_BLOCKS: 1188663207adSDon Brady if (zpool_hdl != NULL) { 1189663207adSDon Brady char state[64] = ""; 1190663207adSDon Brady 1191663207adSDon Brady /* 1192663207adSDon Brady * Issue a warning but do not fail so that 1193663207adSDon Brady * tests for setable properties succeed. 1194663207adSDon Brady */ 1195663207adSDon Brady if (zpool_prop_get_feature(zpool_hdl, 1196663207adSDon Brady "feature@allocation_classes", state, 1197663207adSDon Brady sizeof (state)) != 0 || 1198663207adSDon Brady strcmp(state, ZFS_FEATURE_ACTIVE) != 0) { 1199663207adSDon Brady (void) fprintf(stderr, gettext( 1200663207adSDon Brady "%s: property requires a special " 1201663207adSDon Brady "device in the pool\n"), propname); 1202663207adSDon Brady } 1203663207adSDon Brady } 1204663207adSDon Brady if (intval != 0 && 1205663207adSDon Brady (intval < SPA_MINBLOCKSIZE || 1206663207adSDon Brady intval > SPA_OLD_MAXBLOCKSIZE || !ISP2(intval))) { 1207663207adSDon Brady zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1208663207adSDon Brady "invalid '%s=%d' property: must be zero or " 1209663207adSDon Brady "a power of 2 from 512B to 128K"), propname, 1210663207adSDon Brady intval); 1211663207adSDon Brady (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1212663207adSDon Brady goto error; 1213663207adSDon Brady } 1214663207adSDon Brady break; 1215663207adSDon Brady 12164201a95eSRic Aleshire case ZFS_PROP_MLSLABEL: 12174201a95eSRic Aleshire { 12184201a95eSRic Aleshire /* 12194201a95eSRic Aleshire * Verify the mlslabel string and convert to 12204201a95eSRic Aleshire * internal hex label string. 12214201a95eSRic Aleshire */ 12224201a95eSRic Aleshire 12234201a95eSRic Aleshire m_label_t *new_sl; 12244201a95eSRic Aleshire char *hex = NULL; /* internal label string */ 12254201a95eSRic Aleshire 12264201a95eSRic Aleshire /* Default value is already OK. */ 12274201a95eSRic Aleshire if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0) 12284201a95eSRic Aleshire break; 12294201a95eSRic Aleshire 12304201a95eSRic Aleshire /* Verify the label can be converted to binary form */ 12314201a95eSRic Aleshire if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) || 12324201a95eSRic Aleshire (str_to_label(strval, &new_sl, MAC_LABEL, 12334201a95eSRic Aleshire L_NO_CORRECTION, NULL) == -1)) { 12344201a95eSRic Aleshire goto badlabel; 12354201a95eSRic Aleshire } 12364201a95eSRic Aleshire 12374201a95eSRic Aleshire /* Now translate to hex internal label string */ 12384201a95eSRic Aleshire if (label_to_str(new_sl, &hex, M_INTERNAL, 12394201a95eSRic Aleshire DEF_NAMES) != 0) { 12404201a95eSRic Aleshire if (hex) 12414201a95eSRic Aleshire free(hex); 12424201a95eSRic Aleshire goto badlabel; 12434201a95eSRic Aleshire } 12444201a95eSRic Aleshire m_label_free(new_sl); 12454201a95eSRic Aleshire 12464201a95eSRic Aleshire /* If string is already in internal form, we're done. */ 12474201a95eSRic Aleshire if (strcmp(strval, hex) == 0) { 12484201a95eSRic Aleshire free(hex); 12494201a95eSRic Aleshire break; 12504201a95eSRic Aleshire } 12514201a95eSRic Aleshire 12524201a95eSRic Aleshire /* Replace the label string with the internal form. */ 1253569038c9SRic Aleshire (void) nvlist_remove(ret, zfs_prop_to_name(prop), 12544201a95eSRic Aleshire DATA_TYPE_STRING); 12554201a95eSRic Aleshire verify(nvlist_add_string(ret, zfs_prop_to_name(prop), 12564201a95eSRic Aleshire hex) == 0); 12574201a95eSRic Aleshire free(hex); 12584201a95eSRic Aleshire 12594201a95eSRic Aleshire break; 12604201a95eSRic Aleshire 12614201a95eSRic Aleshire badlabel: 12624201a95eSRic Aleshire zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 12634201a95eSRic Aleshire "invalid mlslabel '%s'"), strval); 12644201a95eSRic Aleshire (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 12654201a95eSRic Aleshire m_label_free(new_sl); /* OK if null */ 12664201a95eSRic Aleshire goto error; 12674201a95eSRic Aleshire 12684201a95eSRic Aleshire } 12694201a95eSRic Aleshire 1270e9dbad6fSeschrock case ZFS_PROP_MOUNTPOINT: 127189eef05eSrm160521 { 127289eef05eSrm160521 namecheck_err_t why; 127389eef05eSrm160521 1274e9dbad6fSeschrock if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 || 1275e9dbad6fSeschrock strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0) 1276e9dbad6fSeschrock break; 1277e9dbad6fSeschrock 127889eef05eSrm160521 if (mountpoint_namecheck(strval, &why)) { 127989eef05eSrm160521 switch (why) { 128089eef05eSrm160521 case NAME_ERR_LEADING_SLASH: 128189eef05eSrm160521 zfs_error_aux(hdl, 128289eef05eSrm160521 dgettext(TEXT_DOMAIN, 1283e9dbad6fSeschrock "'%s' must be an absolute path, " 1284e9dbad6fSeschrock "'none', or 'legacy'"), propname); 128589eef05eSrm160521 break; 128689eef05eSrm160521 case NAME_ERR_TOOLONG: 128789eef05eSrm160521 zfs_error_aux(hdl, 128889eef05eSrm160521 dgettext(TEXT_DOMAIN, 128989eef05eSrm160521 "component of '%s' is too long"), 129089eef05eSrm160521 propname); 129189eef05eSrm160521 break; 129288f61deeSIgor Kozhukhov 129388f61deeSIgor Kozhukhov default: 129488f61deeSIgor Kozhukhov zfs_error_aux(hdl, 129588f61deeSIgor Kozhukhov dgettext(TEXT_DOMAIN, 129688f61deeSIgor Kozhukhov "(%d) not defined"), 129788f61deeSIgor Kozhukhov why); 129888f61deeSIgor Kozhukhov break; 129989eef05eSrm160521 } 1300e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1301e9dbad6fSeschrock goto error; 1302e9dbad6fSeschrock } 130389eef05eSrm160521 } 130489eef05eSrm160521 1305f3861e1aSahl /*FALLTHRU*/ 1306e9dbad6fSeschrock 1307da6c28aaSamw case ZFS_PROP_SHARESMB: 1308f3861e1aSahl case ZFS_PROP_SHARENFS: 1309e9dbad6fSeschrock /* 1310da6c28aaSamw * For the mountpoint and sharenfs or sharesmb 1311da6c28aaSamw * properties, check if it can be set in a 1312da6c28aaSamw * global/non-global zone based on 1313f3861e1aSahl * the zoned property value: 1314fa9e4066Sahrens * 1315fa9e4066Sahrens * global zone non-global zone 1316f3861e1aSahl * -------------------------------------------------- 1317fa9e4066Sahrens * zoned=on mountpoint (no) mountpoint (yes) 13180dfe541eSEvan Layton * sharenfs (no) sharenfs (yes) 13190dfe541eSEvan Layton * sharesmb (no) sharesmb (yes) 1320fa9e4066Sahrens * 1321fa9e4066Sahrens * zoned=off mountpoint (yes) N/A 1322fa9e4066Sahrens * sharenfs (yes) 1323da6c28aaSamw * sharesmb (yes) 1324fa9e4066Sahrens */ 1325e9dbad6fSeschrock if (zoned) { 1326fa9e4066Sahrens if (getzoneid() == GLOBAL_ZONEID) { 132799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1328e9dbad6fSeschrock "'%s' cannot be set on " 1329e9dbad6fSeschrock "dataset in a non-global zone"), 1330e9dbad6fSeschrock propname); 1331e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_ZONED, 1332e9dbad6fSeschrock errbuf); 1333e9dbad6fSeschrock goto error; 1334fa9e4066Sahrens } 1335fa9e4066Sahrens } else if (getzoneid() != GLOBAL_ZONEID) { 1336fa9e4066Sahrens /* 1337fa9e4066Sahrens * If zoned property is 'off', this must be in 133814843421SMatthew Ahrens * a global zone. If not, something is wrong. 1339fa9e4066Sahrens */ 134099653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1341e9dbad6fSeschrock "'%s' cannot be set while dataset " 1342e9dbad6fSeschrock "'zoned' property is set"), propname); 1343e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_ZONED, errbuf); 1344e9dbad6fSeschrock goto error; 1345fa9e4066Sahrens } 1346f3861e1aSahl 134767331909Sdougm /* 134867331909Sdougm * At this point, it is legitimate to set the 134967331909Sdougm * property. Now we want to make sure that the 135067331909Sdougm * property value is valid if it is sharenfs. 135167331909Sdougm */ 1352da6c28aaSamw if ((prop == ZFS_PROP_SHARENFS || 1353da6c28aaSamw prop == ZFS_PROP_SHARESMB) && 135467331909Sdougm strcmp(strval, "on") != 0 && 135567331909Sdougm strcmp(strval, "off") != 0) { 1356da6c28aaSamw zfs_share_proto_t proto; 1357da6c28aaSamw 1358da6c28aaSamw if (prop == ZFS_PROP_SHARESMB) 1359da6c28aaSamw proto = PROTO_SMB; 1360da6c28aaSamw else 1361da6c28aaSamw proto = PROTO_NFS; 136267331909Sdougm 136367331909Sdougm /* 1364da6c28aaSamw * Must be an valid sharing protocol 1365da6c28aaSamw * option string so init the libshare 1366da6c28aaSamw * in order to enable the parser and 1367da6c28aaSamw * then parse the options. We use the 1368da6c28aaSamw * control API since we don't care about 1369da6c28aaSamw * the current configuration and don't 137067331909Sdougm * want the overhead of loading it 137167331909Sdougm * until we actually do something. 137267331909Sdougm */ 137367331909Sdougm 137467331909Sdougm if (zfs_init_libshare(hdl, 137567331909Sdougm SA_INIT_CONTROL_API) != SA_OK) { 1376fac3008cSeschrock /* 1377fac3008cSeschrock * An error occurred so we can't do 1378fac3008cSeschrock * anything 1379fac3008cSeschrock */ 138067331909Sdougm zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 138167331909Sdougm "'%s' cannot be set: problem " 138267331909Sdougm "in share initialization"), 138367331909Sdougm propname); 1384fac3008cSeschrock (void) zfs_error(hdl, EZFS_BADPROP, 1385fac3008cSeschrock errbuf); 138667331909Sdougm goto error; 138767331909Sdougm } 138867331909Sdougm 1389da6c28aaSamw if (zfs_parse_options(strval, proto) != SA_OK) { 139067331909Sdougm /* 139167331909Sdougm * There was an error in parsing so 139267331909Sdougm * deal with it by issuing an error 139367331909Sdougm * message and leaving after 139467331909Sdougm * uninitializing the the libshare 139567331909Sdougm * interface. 139667331909Sdougm */ 139767331909Sdougm zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 139867331909Sdougm "'%s' cannot be set to invalid " 139967331909Sdougm "options"), propname); 1400fac3008cSeschrock (void) zfs_error(hdl, EZFS_BADPROP, 1401fac3008cSeschrock errbuf); 140267331909Sdougm zfs_uninit_libshare(hdl); 140367331909Sdougm goto error; 140467331909Sdougm } 140567331909Sdougm zfs_uninit_libshare(hdl); 140667331909Sdougm } 140767331909Sdougm 1408f3861e1aSahl break; 140988f61deeSIgor Kozhukhov 1410eb633035STom Caputi case ZFS_PROP_KEYLOCATION: 1411eb633035STom Caputi if (!zfs_prop_valid_keylocation(strval, B_FALSE)) { 1412eb633035STom Caputi zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1413eb633035STom Caputi "invalid keylocation")); 1414eb633035STom Caputi (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1415eb633035STom Caputi goto error; 1416eb633035STom Caputi } 1417eb633035STom Caputi 1418eb633035STom Caputi if (zhp != NULL) { 1419eb633035STom Caputi uint64_t crypt = 1420eb633035STom Caputi zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION); 1421eb633035STom Caputi 1422eb633035STom Caputi if (crypt == ZIO_CRYPT_OFF && 1423eb633035STom Caputi strcmp(strval, "none") != 0) { 1424eb633035STom Caputi zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1425eb633035STom Caputi "keylocation must be 'none' " 1426eb633035STom Caputi "for unencrypted datasets")); 1427eb633035STom Caputi (void) zfs_error(hdl, EZFS_BADPROP, 1428eb633035STom Caputi errbuf); 1429eb633035STom Caputi goto error; 1430eb633035STom Caputi } else if (crypt != ZIO_CRYPT_OFF && 1431eb633035STom Caputi strcmp(strval, "none") == 0) { 1432eb633035STom Caputi zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1433eb633035STom Caputi "keylocation must not be 'none' " 1434eb633035STom Caputi "for encrypted datasets")); 1435eb633035STom Caputi (void) zfs_error(hdl, EZFS_BADPROP, 1436eb633035STom Caputi errbuf); 1437eb633035STom Caputi goto error; 1438eb633035STom Caputi } 1439eb633035STom Caputi } 1440eb633035STom Caputi break; 1441eb633035STom Caputi 1442eb633035STom Caputi case ZFS_PROP_PBKDF2_ITERS: 1443eb633035STom Caputi if (intval < MIN_PBKDF2_ITERATIONS) { 1444eb633035STom Caputi zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1445eb633035STom Caputi "minimum pbkdf2 iterations is %u"), 1446eb633035STom Caputi MIN_PBKDF2_ITERATIONS); 1447eb633035STom Caputi (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1448eb633035STom Caputi goto error; 1449eb633035STom Caputi } 1450eb633035STom Caputi break; 1451eb633035STom Caputi 1452da6c28aaSamw case ZFS_PROP_UTF8ONLY: 1453da6c28aaSamw chosen_utf = (int)intval; 1454da6c28aaSamw break; 145588f61deeSIgor Kozhukhov 1456da6c28aaSamw case ZFS_PROP_NORMALIZE: 1457da6c28aaSamw chosen_normal = (int)intval; 1458da6c28aaSamw break; 145988f61deeSIgor Kozhukhov 146088f61deeSIgor Kozhukhov default: 146188f61deeSIgor Kozhukhov break; 1462fa9e4066Sahrens } 1463fa9e4066Sahrens 1464e9dbad6fSeschrock /* 1465e9dbad6fSeschrock * For changes to existing volumes, we have some additional 1466e9dbad6fSeschrock * checks to enforce. 1467e9dbad6fSeschrock */ 1468e9dbad6fSeschrock if (type == ZFS_TYPE_VOLUME && zhp != NULL) { 1469e9dbad6fSeschrock uint64_t volsize = zfs_prop_get_int(zhp, 1470e9dbad6fSeschrock ZFS_PROP_VOLSIZE); 1471e9dbad6fSeschrock uint64_t blocksize = zfs_prop_get_int(zhp, 1472e9dbad6fSeschrock ZFS_PROP_VOLBLOCKSIZE); 1473e9dbad6fSeschrock char buf[64]; 1474e9dbad6fSeschrock 1475e9dbad6fSeschrock switch (prop) { 1476e9dbad6fSeschrock case ZFS_PROP_RESERVATION: 1477e9dbad6fSeschrock if (intval > volsize) { 1478e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1479e9dbad6fSeschrock "'%s' is greater than current " 1480e9dbad6fSeschrock "volume size"), propname); 1481e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_BADPROP, 1482e9dbad6fSeschrock errbuf); 1483e9dbad6fSeschrock goto error; 1484e9dbad6fSeschrock } 1485e9dbad6fSeschrock break; 1486e9dbad6fSeschrock 14871c10ae76SMike Gerdts case ZFS_PROP_REFRESERVATION: 14881c10ae76SMike Gerdts if (intval > volsize && intval != UINT64_MAX) { 14891c10ae76SMike Gerdts zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 14901c10ae76SMike Gerdts "'%s' is greater than current " 14911c10ae76SMike Gerdts "volume size"), propname); 14921c10ae76SMike Gerdts (void) zfs_error(hdl, EZFS_BADPROP, 14931c10ae76SMike Gerdts errbuf); 14941c10ae76SMike Gerdts goto error; 14951c10ae76SMike Gerdts } 14961c10ae76SMike Gerdts break; 14971c10ae76SMike Gerdts 1498e9dbad6fSeschrock case ZFS_PROP_VOLSIZE: 1499e9dbad6fSeschrock if (intval % blocksize != 0) { 1500e9dbad6fSeschrock zfs_nicenum(blocksize, buf, 1501e9dbad6fSeschrock sizeof (buf)); 1502e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1503e9dbad6fSeschrock "'%s' must be a multiple of " 1504e9dbad6fSeschrock "volume block size (%s)"), 1505e9dbad6fSeschrock propname, buf); 1506e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_BADPROP, 1507e9dbad6fSeschrock errbuf); 1508e9dbad6fSeschrock goto error; 1509e9dbad6fSeschrock } 1510e9dbad6fSeschrock 1511e9dbad6fSeschrock if (intval == 0) { 1512e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1513e9dbad6fSeschrock "'%s' cannot be zero"), 1514e9dbad6fSeschrock propname); 1515e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_BADPROP, 1516e9dbad6fSeschrock errbuf); 1517e9dbad6fSeschrock goto error; 1518e9dbad6fSeschrock } 1519f3861e1aSahl break; 152088f61deeSIgor Kozhukhov 152188f61deeSIgor Kozhukhov default: 152288f61deeSIgor Kozhukhov break; 1523e9dbad6fSeschrock } 1524e9dbad6fSeschrock } 1525eb633035STom Caputi 1526eb633035STom Caputi /* check encryption properties */ 1527eb633035STom Caputi if (zhp != NULL) { 1528eb633035STom Caputi int64_t crypt = zfs_prop_get_int(zhp, 1529eb633035STom Caputi ZFS_PROP_ENCRYPTION); 1530eb633035STom Caputi 1531eb633035STom Caputi switch (prop) { 1532eb633035STom Caputi case ZFS_PROP_COPIES: 1533eb633035STom Caputi if (crypt != ZIO_CRYPT_OFF && intval > 2) { 1534eb633035STom Caputi zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1535eb633035STom Caputi "encrypted datasets cannot have " 1536eb633035STom Caputi "3 copies")); 1537eb633035STom Caputi (void) zfs_error(hdl, EZFS_BADPROP, 1538eb633035STom Caputi errbuf); 1539eb633035STom Caputi goto error; 1540eb633035STom Caputi } 1541eb633035STom Caputi break; 1542eb633035STom Caputi default: 1543eb633035STom Caputi break; 1544eb633035STom Caputi } 1545eb633035STom Caputi } 1546e9dbad6fSeschrock } 1547e9dbad6fSeschrock 1548e9dbad6fSeschrock /* 1549da6c28aaSamw * If normalization was chosen, but no UTF8 choice was made, 1550da6c28aaSamw * enforce rejection of non-UTF8 names. 1551da6c28aaSamw * 1552da6c28aaSamw * If normalization was chosen, but rejecting non-UTF8 names 1553da6c28aaSamw * was explicitly not chosen, it is an error. 1554da6c28aaSamw */ 1555de8267e0Stimh if (chosen_normal > 0 && chosen_utf < 0) { 1556da6c28aaSamw if (nvlist_add_uint64(ret, 1557da6c28aaSamw zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) { 1558da6c28aaSamw (void) no_memory(hdl); 1559da6c28aaSamw goto error; 1560da6c28aaSamw } 1561de8267e0Stimh } else if (chosen_normal > 0 && chosen_utf == 0) { 1562da6c28aaSamw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1563da6c28aaSamw "'%s' must be set 'on' if normalization chosen"), 1564da6c28aaSamw zfs_prop_to_name(ZFS_PROP_UTF8ONLY)); 1565da6c28aaSamw (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1566da6c28aaSamw goto error; 1567da6c28aaSamw } 1568e9dbad6fSeschrock return (ret); 1569e9dbad6fSeschrock 1570e9dbad6fSeschrock error: 1571e9dbad6fSeschrock nvlist_free(ret); 1572e9dbad6fSeschrock return (NULL); 1573e9dbad6fSeschrock } 1574e9dbad6fSeschrock 157536db6475SEric Taylor int 157636db6475SEric Taylor zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl) 157736db6475SEric Taylor { 157836db6475SEric Taylor uint64_t old_volsize; 157936db6475SEric Taylor uint64_t new_volsize; 158036db6475SEric Taylor uint64_t old_reservation; 158136db6475SEric Taylor uint64_t new_reservation; 158236db6475SEric Taylor zfs_prop_t resv_prop; 1583c61ea566SGeorge Wilson nvlist_t *props; 1584b73ccab0SMike Gerdts zpool_handle_t *zph = zpool_handle(zhp); 158536db6475SEric Taylor 158636db6475SEric Taylor /* 158736db6475SEric Taylor * If this is an existing volume, and someone is setting the volsize, 158836db6475SEric Taylor * make sure that it matches the reservation, or add it if necessary. 158936db6475SEric Taylor */ 159036db6475SEric Taylor old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 159136db6475SEric Taylor if (zfs_which_resv_prop(zhp, &resv_prop) < 0) 159236db6475SEric Taylor return (-1); 159336db6475SEric Taylor old_reservation = zfs_prop_get_int(zhp, resv_prop); 1594c61ea566SGeorge Wilson 1595c61ea566SGeorge Wilson props = fnvlist_alloc(); 1596c61ea566SGeorge Wilson fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 1597c61ea566SGeorge Wilson zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE)); 1598c61ea566SGeorge Wilson 1599b73ccab0SMike Gerdts if ((zvol_volsize_to_reservation(zph, old_volsize, props) != 1600c61ea566SGeorge Wilson old_reservation) || nvlist_exists(nvl, 1601c61ea566SGeorge Wilson zfs_prop_to_name(resv_prop))) { 1602c61ea566SGeorge Wilson fnvlist_free(props); 160336db6475SEric Taylor return (0); 160436db6475SEric Taylor } 160536db6475SEric Taylor if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE), 1606c61ea566SGeorge Wilson &new_volsize) != 0) { 1607c61ea566SGeorge Wilson fnvlist_free(props); 160836db6475SEric Taylor return (-1); 1609c61ea566SGeorge Wilson } 1610b73ccab0SMike Gerdts new_reservation = zvol_volsize_to_reservation(zph, new_volsize, props); 1611c61ea566SGeorge Wilson fnvlist_free(props); 1612c61ea566SGeorge Wilson 161336db6475SEric Taylor if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop), 161436db6475SEric Taylor new_reservation) != 0) { 161536db6475SEric Taylor (void) no_memory(zhp->zfs_hdl); 161636db6475SEric Taylor return (-1); 161736db6475SEric Taylor } 161836db6475SEric Taylor return (1); 161936db6475SEric Taylor } 162036db6475SEric Taylor 16211c10ae76SMike Gerdts /* 16221c10ae76SMike Gerdts * Helper for 'zfs {set|clone} refreservation=auto'. Must be called after 16231c10ae76SMike Gerdts * zfs_valid_proplist(), as it is what sets the UINT64_MAX sentinal value. 16241c10ae76SMike Gerdts * Return codes must match zfs_add_synthetic_resv(). 16251c10ae76SMike Gerdts */ 16261c10ae76SMike Gerdts static int 16271c10ae76SMike Gerdts zfs_fix_auto_resv(zfs_handle_t *zhp, nvlist_t *nvl) 16281c10ae76SMike Gerdts { 16291c10ae76SMike Gerdts uint64_t volsize; 16301c10ae76SMike Gerdts uint64_t resvsize; 16311c10ae76SMike Gerdts zfs_prop_t prop; 16321c10ae76SMike Gerdts nvlist_t *props; 16331c10ae76SMike Gerdts 16341c10ae76SMike Gerdts if (!ZFS_IS_VOLUME(zhp)) { 16351c10ae76SMike Gerdts return (0); 16361c10ae76SMike Gerdts } 16371c10ae76SMike Gerdts 16381c10ae76SMike Gerdts if (zfs_which_resv_prop(zhp, &prop) != 0) { 16391c10ae76SMike Gerdts return (-1); 16401c10ae76SMike Gerdts } 16411c10ae76SMike Gerdts 16421c10ae76SMike Gerdts if (prop != ZFS_PROP_REFRESERVATION) { 16431c10ae76SMike Gerdts return (0); 16441c10ae76SMike Gerdts } 16451c10ae76SMike Gerdts 16461c10ae76SMike Gerdts if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(prop), &resvsize) != 0) { 16471c10ae76SMike Gerdts /* No value being set, so it can't be "auto" */ 16481c10ae76SMike Gerdts return (0); 16491c10ae76SMike Gerdts } 16501c10ae76SMike Gerdts if (resvsize != UINT64_MAX) { 16511c10ae76SMike Gerdts /* Being set to a value other than "auto" */ 16521c10ae76SMike Gerdts return (0); 16531c10ae76SMike Gerdts } 16541c10ae76SMike Gerdts 16551c10ae76SMike Gerdts props = fnvlist_alloc(); 16561c10ae76SMike Gerdts 16571c10ae76SMike Gerdts fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 16581c10ae76SMike Gerdts zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE)); 16591c10ae76SMike Gerdts 16601c10ae76SMike Gerdts if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE), 16611c10ae76SMike Gerdts &volsize) != 0) { 16621c10ae76SMike Gerdts volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 16631c10ae76SMike Gerdts } 16641c10ae76SMike Gerdts 1665b73ccab0SMike Gerdts resvsize = zvol_volsize_to_reservation(zpool_handle(zhp), volsize, 1666b73ccab0SMike Gerdts props); 16671c10ae76SMike Gerdts fnvlist_free(props); 16681c10ae76SMike Gerdts 16691c10ae76SMike Gerdts (void) nvlist_remove_all(nvl, zfs_prop_to_name(prop)); 16701c10ae76SMike Gerdts if (nvlist_add_uint64(nvl, zfs_prop_to_name(prop), resvsize) != 0) { 16711c10ae76SMike Gerdts (void) no_memory(zhp->zfs_hdl); 16721c10ae76SMike Gerdts return (-1); 16731c10ae76SMike Gerdts } 16741c10ae76SMike Gerdts return (1); 16751c10ae76SMike Gerdts } 16761c10ae76SMike Gerdts 167792241e0bSTom Erickson void 167892241e0bSTom Erickson zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err, 167992241e0bSTom Erickson char *errbuf) 168092241e0bSTom Erickson { 168192241e0bSTom Erickson switch (err) { 168292241e0bSTom Erickson 168392241e0bSTom Erickson case ENOSPC: 168492241e0bSTom Erickson /* 168592241e0bSTom Erickson * For quotas and reservations, ENOSPC indicates 168692241e0bSTom Erickson * something different; setting a quota or reservation 168792241e0bSTom Erickson * doesn't use any disk space. 168892241e0bSTom Erickson */ 168992241e0bSTom Erickson switch (prop) { 169092241e0bSTom Erickson case ZFS_PROP_QUOTA: 169192241e0bSTom Erickson case ZFS_PROP_REFQUOTA: 169292241e0bSTom Erickson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 169392241e0bSTom Erickson "size is less than current used or " 169492241e0bSTom Erickson "reserved space")); 169592241e0bSTom Erickson (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 169692241e0bSTom Erickson break; 169792241e0bSTom Erickson 169892241e0bSTom Erickson case ZFS_PROP_RESERVATION: 169992241e0bSTom Erickson case ZFS_PROP_REFRESERVATION: 170092241e0bSTom Erickson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 170192241e0bSTom Erickson "size is greater than available space")); 170292241e0bSTom Erickson (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 170392241e0bSTom Erickson break; 170492241e0bSTom Erickson 170592241e0bSTom Erickson default: 170692241e0bSTom Erickson (void) zfs_standard_error(hdl, err, errbuf); 170792241e0bSTom Erickson break; 170892241e0bSTom Erickson } 170992241e0bSTom Erickson break; 171092241e0bSTom Erickson 171192241e0bSTom Erickson case EBUSY: 171292241e0bSTom Erickson (void) zfs_standard_error(hdl, EBUSY, errbuf); 171392241e0bSTom Erickson break; 171492241e0bSTom Erickson 171592241e0bSTom Erickson case EROFS: 171692241e0bSTom Erickson (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf); 171792241e0bSTom Erickson break; 171892241e0bSTom Erickson 17196fdcb3d1SWill Andrews case E2BIG: 17206fdcb3d1SWill Andrews zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 17216fdcb3d1SWill Andrews "property value too long")); 17226fdcb3d1SWill Andrews (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 17236fdcb3d1SWill Andrews break; 17246fdcb3d1SWill Andrews 172592241e0bSTom Erickson case ENOTSUP: 172692241e0bSTom Erickson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 172792241e0bSTom Erickson "pool and or dataset must be upgraded to set this " 172892241e0bSTom Erickson "property or value")); 172992241e0bSTom Erickson (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 173092241e0bSTom Erickson break; 173192241e0bSTom Erickson 173292241e0bSTom Erickson case ERANGE: 1733b5152584SMatthew Ahrens if (prop == ZFS_PROP_COMPRESSION || 1734b5152584SMatthew Ahrens prop == ZFS_PROP_RECORDSIZE) { 173592241e0bSTom Erickson (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 173692241e0bSTom Erickson "property setting is not allowed on " 173792241e0bSTom Erickson "bootable datasets")); 173892241e0bSTom Erickson (void) zfs_error(hdl, EZFS_NOTSUP, errbuf); 173945818ee1SMatthew Ahrens } else if (prop == ZFS_PROP_CHECKSUM || 174045818ee1SMatthew Ahrens prop == ZFS_PROP_DEDUP) { 174145818ee1SMatthew Ahrens (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 174245818ee1SMatthew Ahrens "property setting is not allowed on " 174345818ee1SMatthew Ahrens "root pools")); 174445818ee1SMatthew Ahrens (void) zfs_error(hdl, EZFS_NOTSUP, errbuf); 174592241e0bSTom Erickson } else { 174692241e0bSTom Erickson (void) zfs_standard_error(hdl, err, errbuf); 174792241e0bSTom Erickson } 174892241e0bSTom Erickson break; 174992241e0bSTom Erickson 1750ab003da8SJim Dunham case EINVAL: 1751ab003da8SJim Dunham if (prop == ZPROP_INVAL) { 1752ab003da8SJim Dunham (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1753ab003da8SJim Dunham } else { 1754ab003da8SJim Dunham (void) zfs_standard_error(hdl, err, errbuf); 1755ab003da8SJim Dunham } 1756ab003da8SJim Dunham break; 1757ab003da8SJim Dunham 1758eb633035STom Caputi case EACCES: 1759eb633035STom Caputi if (prop == ZFS_PROP_KEYLOCATION) { 1760eb633035STom Caputi zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1761eb633035STom Caputi "keylocation may only be set on encryption roots")); 1762eb633035STom Caputi (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1763eb633035STom Caputi } else { 1764eb633035STom Caputi (void) zfs_standard_error(hdl, err, errbuf); 1765eb633035STom Caputi } 1766eb633035STom Caputi break; 1767eb633035STom Caputi 176892241e0bSTom Erickson case EOVERFLOW: 176992241e0bSTom Erickson /* 177092241e0bSTom Erickson * This platform can't address a volume this big. 177192241e0bSTom Erickson */ 177292241e0bSTom Erickson #ifdef _ILP32 177392241e0bSTom Erickson if (prop == ZFS_PROP_VOLSIZE) { 177492241e0bSTom Erickson (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf); 177592241e0bSTom Erickson break; 177692241e0bSTom Erickson } 177792241e0bSTom Erickson #endif 177892241e0bSTom Erickson /* FALLTHROUGH */ 177992241e0bSTom Erickson default: 178092241e0bSTom Erickson (void) zfs_standard_error(hdl, err, errbuf); 178192241e0bSTom Erickson } 178292241e0bSTom Erickson } 178392241e0bSTom Erickson 1784e9dbad6fSeschrock /* 1785e9dbad6fSeschrock * Given a property name and value, set the property for the given dataset. 1786e9dbad6fSeschrock */ 1787e9dbad6fSeschrock int 1788e9dbad6fSeschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval) 1789e9dbad6fSeschrock { 1790e9dbad6fSeschrock int ret = -1; 1791e9dbad6fSeschrock char errbuf[1024]; 1792e9dbad6fSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 179330925561SChris Williamson nvlist_t *nvl = NULL; 1794e9dbad6fSeschrock 1795e9dbad6fSeschrock (void) snprintf(errbuf, sizeof (errbuf), 1796e9dbad6fSeschrock dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 1797e9dbad6fSeschrock zhp->zfs_name); 1798e9dbad6fSeschrock 1799e9dbad6fSeschrock if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 || 1800e9dbad6fSeschrock nvlist_add_string(nvl, propname, propval) != 0) { 1801e9dbad6fSeschrock (void) no_memory(hdl); 1802e9dbad6fSeschrock goto error; 1803e9dbad6fSeschrock } 1804e9dbad6fSeschrock 180530925561SChris Williamson ret = zfs_prop_set_list(zhp, nvl); 180630925561SChris Williamson 180730925561SChris Williamson error: 180830925561SChris Williamson nvlist_free(nvl); 180930925561SChris Williamson return (ret); 181030925561SChris Williamson } 181130925561SChris Williamson 181230925561SChris Williamson 181330925561SChris Williamson 181430925561SChris Williamson /* 181530925561SChris Williamson * Given an nvlist of property names and values, set the properties for the 181630925561SChris Williamson * given dataset. 181730925561SChris Williamson */ 181830925561SChris Williamson int 181930925561SChris Williamson zfs_prop_set_list(zfs_handle_t *zhp, nvlist_t *props) 182030925561SChris Williamson { 182130925561SChris Williamson zfs_cmd_t zc = { 0 }; 182230925561SChris Williamson int ret = -1; 182330925561SChris Williamson prop_changelist_t **cls = NULL; 182430925561SChris Williamson int cl_idx; 182530925561SChris Williamson char errbuf[1024]; 182630925561SChris Williamson libzfs_handle_t *hdl = zhp->zfs_hdl; 182730925561SChris Williamson nvlist_t *nvl; 182830925561SChris Williamson int nvl_len; 1829f83b46baSPaul Dagnelie int added_resv = 0; 183030925561SChris Williamson 183130925561SChris Williamson (void) snprintf(errbuf, sizeof (errbuf), 183230925561SChris Williamson dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 183330925561SChris Williamson zhp->zfs_name); 183430925561SChris Williamson 183530925561SChris Williamson if ((nvl = zfs_valid_proplist(hdl, zhp->zfs_type, props, 1836e9316f76SJoe Stein zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, zhp->zpool_hdl, 1837eb633035STom Caputi B_FALSE, errbuf)) == NULL) 1838e9dbad6fSeschrock goto error; 1839990b4856Slling 184030925561SChris Williamson /* 184130925561SChris Williamson * We have to check for any extra properties which need to be added 184230925561SChris Williamson * before computing the length of the nvlist. 184330925561SChris Williamson */ 184430925561SChris Williamson for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL); 184530925561SChris Williamson elem != NULL; 184630925561SChris Williamson elem = nvlist_next_nvpair(nvl, elem)) { 184730925561SChris Williamson if (zfs_name_to_prop(nvpair_name(elem)) == ZFS_PROP_VOLSIZE && 184830925561SChris Williamson (added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1) { 184930925561SChris Williamson goto error; 185030925561SChris Williamson } 185130925561SChris Williamson } 18521c10ae76SMike Gerdts 18531c10ae76SMike Gerdts if (added_resv != 1 && 18541c10ae76SMike Gerdts (added_resv = zfs_fix_auto_resv(zhp, nvl)) == -1) { 18551c10ae76SMike Gerdts goto error; 18561c10ae76SMike Gerdts } 18571c10ae76SMike Gerdts 185830925561SChris Williamson /* 185930925561SChris Williamson * Check how many properties we're setting and allocate an array to 186030925561SChris Williamson * store changelist pointers for postfix(). 186130925561SChris Williamson */ 186230925561SChris Williamson nvl_len = 0; 186330925561SChris Williamson for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL); 186430925561SChris Williamson elem != NULL; 186530925561SChris Williamson elem = nvlist_next_nvpair(nvl, elem)) 186630925561SChris Williamson nvl_len++; 186730925561SChris Williamson if ((cls = calloc(nvl_len, sizeof (prop_changelist_t *))) == NULL) 186830925561SChris Williamson goto error; 1869e9dbad6fSeschrock 187030925561SChris Williamson cl_idx = 0; 187130925561SChris Williamson for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL); 187230925561SChris Williamson elem != NULL; 187330925561SChris Williamson elem = nvlist_next_nvpair(nvl, elem)) { 1874e9dbad6fSeschrock 187530925561SChris Williamson zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem)); 187630925561SChris Williamson 187730925561SChris Williamson assert(cl_idx < nvl_len); 187830925561SChris Williamson /* 187930925561SChris Williamson * We don't want to unmount & remount the dataset when changing 188030925561SChris Williamson * its canmount property to 'on' or 'noauto'. We only use 188130925561SChris Williamson * the changelist logic to unmount when setting canmount=off. 188230925561SChris Williamson */ 1883c079fa4dSAndriy Gapon if (prop != ZFS_PROP_CANMOUNT || 1884c079fa4dSAndriy Gapon (fnvpair_value_uint64(elem) == ZFS_CANMOUNT_OFF && 1885c079fa4dSAndriy Gapon zfs_is_mounted(zhp, NULL))) { 188630925561SChris Williamson cls[cl_idx] = changelist_gather(zhp, prop, 0, 0); 188730925561SChris Williamson if (cls[cl_idx] == NULL) 188836db6475SEric Taylor goto error; 188936db6475SEric Taylor } 189036db6475SEric Taylor 189130925561SChris Williamson if (prop == ZFS_PROP_MOUNTPOINT && 189230925561SChris Williamson changelist_haszonedchild(cls[cl_idx])) { 189399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1894fa9e4066Sahrens "child dataset with inherited mountpoint is used " 189599653d4eSeschrock "in a non-global zone")); 189699653d4eSeschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1897fa9e4066Sahrens goto error; 1898fa9e4066Sahrens } 1899fa9e4066Sahrens 190030925561SChris Williamson if (cls[cl_idx] != NULL && 190130925561SChris Williamson (ret = changelist_prefix(cls[cl_idx])) != 0) 1902fa9e4066Sahrens goto error; 1903fa9e4066Sahrens 190430925561SChris Williamson cl_idx++; 190530925561SChris Williamson } 190630925561SChris Williamson assert(cl_idx == nvl_len); 190730925561SChris Williamson 1908fa9e4066Sahrens /* 190930925561SChris Williamson * Execute the corresponding ioctl() to set this list of properties. 1910fa9e4066Sahrens */ 1911fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1912fa9e4066Sahrens 191330925561SChris Williamson if ((ret = zcmd_write_src_nvlist(hdl, &zc, nvl)) != 0 || 191430925561SChris Williamson (ret = zcmd_alloc_dst_nvlist(hdl, &zc, 0)) != 0) 1915e9dbad6fSeschrock goto error; 1916e9dbad6fSeschrock 1917ecd6cf80Smarks ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc); 1918743a77edSAlan Wright 1919fa9e4066Sahrens if (ret != 0) { 1920f62db44dSAndrew Stormont if (zc.zc_nvlist_dst_filled == B_FALSE) { 1921f62db44dSAndrew Stormont (void) zfs_standard_error(hdl, errno, errbuf); 1922f62db44dSAndrew Stormont goto error; 1923f62db44dSAndrew Stormont } 1924f62db44dSAndrew Stormont 192530925561SChris Williamson /* Get the list of unset properties back and report them. */ 192630925561SChris Williamson nvlist_t *errorprops = NULL; 192730925561SChris Williamson if (zcmd_read_dst_nvlist(hdl, &zc, &errorprops) != 0) 192830925561SChris Williamson goto error; 1929f62db44dSAndrew Stormont for (nvpair_t *elem = nvlist_next_nvpair(errorprops, NULL); 193030925561SChris Williamson elem != NULL; 1931f62db44dSAndrew Stormont elem = nvlist_next_nvpair(errorprops, elem)) { 193230925561SChris Williamson zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem)); 193392241e0bSTom Erickson zfs_setprop_error(hdl, prop, errno, errbuf); 193430925561SChris Williamson } 193530925561SChris Williamson nvlist_free(errorprops); 193630925561SChris Williamson 193736db6475SEric Taylor if (added_resv && errno == ENOSPC) { 193836db6475SEric Taylor /* clean up the volsize property we tried to set */ 193936db6475SEric Taylor uint64_t old_volsize = zfs_prop_get_int(zhp, 194036db6475SEric Taylor ZFS_PROP_VOLSIZE); 194136db6475SEric Taylor nvlist_free(nvl); 194230925561SChris Williamson nvl = NULL; 194336db6475SEric Taylor zcmd_free_nvlists(&zc); 194430925561SChris Williamson 194536db6475SEric Taylor if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) 194636db6475SEric Taylor goto error; 194736db6475SEric Taylor if (nvlist_add_uint64(nvl, 194836db6475SEric Taylor zfs_prop_to_name(ZFS_PROP_VOLSIZE), 194936db6475SEric Taylor old_volsize) != 0) 195036db6475SEric Taylor goto error; 195136db6475SEric Taylor if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0) 195236db6475SEric Taylor goto error; 195336db6475SEric Taylor (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc); 195436db6475SEric Taylor } 1955fa9e4066Sahrens } else { 195630925561SChris Williamson for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) { 195730925561SChris Williamson if (cls[cl_idx] != NULL) { 195830925561SChris Williamson int clp_err = changelist_postfix(cls[cl_idx]); 195930925561SChris Williamson if (clp_err != 0) 196030925561SChris Williamson ret = clp_err; 196130925561SChris Williamson } 196230925561SChris Williamson } 1963a227b7f4Shs24103 1964fa9e4066Sahrens /* 1965fa9e4066Sahrens * Refresh the statistics so the new property value 1966fa9e4066Sahrens * is reflected. 1967fa9e4066Sahrens */ 1968a227b7f4Shs24103 if (ret == 0) 1969fa9e4066Sahrens (void) get_stats(zhp); 1970fa9e4066Sahrens } 1971fa9e4066Sahrens 1972fa9e4066Sahrens error: 1973e9dbad6fSeschrock nvlist_free(nvl); 1974e9dbad6fSeschrock zcmd_free_nvlists(&zc); 197530925561SChris Williamson if (cls != NULL) { 197630925561SChris Williamson for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) { 197730925561SChris Williamson if (cls[cl_idx] != NULL) 197830925561SChris Williamson changelist_free(cls[cl_idx]); 197930925561SChris Williamson } 198030925561SChris Williamson free(cls); 198130925561SChris Williamson } 1982fa9e4066Sahrens return (ret); 1983fa9e4066Sahrens } 1984fa9e4066Sahrens 1985fa9e4066Sahrens /* 198692241e0bSTom Erickson * Given a property, inherit the value from the parent dataset, or if received 198792241e0bSTom Erickson * is TRUE, revert to the received value, if any. 1988fa9e4066Sahrens */ 1989fa9e4066Sahrens int 199092241e0bSTom Erickson zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received) 1991fa9e4066Sahrens { 1992fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 1993fa9e4066Sahrens int ret; 1994fa9e4066Sahrens prop_changelist_t *cl; 199599653d4eSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 199699653d4eSeschrock char errbuf[1024]; 1997e9dbad6fSeschrock zfs_prop_t prop; 199899653d4eSeschrock 199999653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 200099653d4eSeschrock "cannot inherit %s for '%s'"), propname, zhp->zfs_name); 2001fa9e4066Sahrens 200292241e0bSTom Erickson zc.zc_cookie = received; 2003990b4856Slling if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) { 2004e9dbad6fSeschrock /* 2005e9dbad6fSeschrock * For user properties, the amount of work we have to do is very 2006e9dbad6fSeschrock * small, so just do it here. 2007e9dbad6fSeschrock */ 2008e9dbad6fSeschrock if (!zfs_prop_user(propname)) { 2009e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2010e9dbad6fSeschrock "invalid property")); 2011e9dbad6fSeschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 2012e9dbad6fSeschrock } 2013e9dbad6fSeschrock 2014e9dbad6fSeschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2015e9dbad6fSeschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 2016e9dbad6fSeschrock 2017e45ce728Sahrens if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0) 2018e9dbad6fSeschrock return (zfs_standard_error(hdl, errno, errbuf)); 2019e9dbad6fSeschrock 2020e9dbad6fSeschrock return (0); 2021e9dbad6fSeschrock } 2022e9dbad6fSeschrock 2023fa9e4066Sahrens /* 2024fa9e4066Sahrens * Verify that this property is inheritable. 2025fa9e4066Sahrens */ 202699653d4eSeschrock if (zfs_prop_readonly(prop)) 202799653d4eSeschrock return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf)); 2028fa9e4066Sahrens 202992241e0bSTom Erickson if (!zfs_prop_inheritable(prop) && !received) 203099653d4eSeschrock return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf)); 2031fa9e4066Sahrens 2032fa9e4066Sahrens /* 2033fa9e4066Sahrens * Check to see if the value applies to this type 2034fa9e4066Sahrens */ 203599653d4eSeschrock if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 203699653d4eSeschrock return (zfs_error(hdl, EZFS_PROPTYPE, errbuf)); 2037fa9e4066Sahrens 2038bf7c2d40Srm160521 /* 203936db6475SEric Taylor * Normalize the name, to get rid of shorthand abbreviations. 2040bf7c2d40Srm160521 */ 2041bf7c2d40Srm160521 propname = zfs_prop_to_name(prop); 2042fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2043e9dbad6fSeschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 2044fa9e4066Sahrens 2045fa9e4066Sahrens if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID && 2046fa9e4066Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 204799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 204899653d4eSeschrock "dataset is used in a non-global zone")); 204999653d4eSeschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 2050fa9e4066Sahrens } 2051fa9e4066Sahrens 2052fa9e4066Sahrens /* 2053fa9e4066Sahrens * Determine datasets which will be affected by this change, if any. 2054fa9e4066Sahrens */ 20550069fd67STim Haley if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL) 2056fa9e4066Sahrens return (-1); 2057fa9e4066Sahrens 2058fa9e4066Sahrens if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 205999653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 206099653d4eSeschrock "child dataset with inherited mountpoint is used " 206199653d4eSeschrock "in a non-global zone")); 206299653d4eSeschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 2063fa9e4066Sahrens goto error; 2064fa9e4066Sahrens } 2065fa9e4066Sahrens 2066fa9e4066Sahrens if ((ret = changelist_prefix(cl)) != 0) 2067fa9e4066Sahrens goto error; 2068fa9e4066Sahrens 2069e45ce728Sahrens if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) { 207099653d4eSeschrock return (zfs_standard_error(hdl, errno, errbuf)); 2071fa9e4066Sahrens } else { 2072fa9e4066Sahrens 2073efc555ebSnd150628 if ((ret = changelist_postfix(cl)) != 0) 2074fa9e4066Sahrens goto error; 2075fa9e4066Sahrens 2076fa9e4066Sahrens /* 2077fa9e4066Sahrens * Refresh the statistics so the new property is reflected. 2078fa9e4066Sahrens */ 2079fa9e4066Sahrens (void) get_stats(zhp); 2080fa9e4066Sahrens } 2081fa9e4066Sahrens 2082fa9e4066Sahrens error: 2083fa9e4066Sahrens changelist_free(cl); 2084fa9e4066Sahrens return (ret); 2085fa9e4066Sahrens } 2086fa9e4066Sahrens 2087fa9e4066Sahrens /* 20887f7322feSeschrock * True DSL properties are stored in an nvlist. The following two functions 20897f7322feSeschrock * extract them appropriately. 20907f7322feSeschrock */ 20917f7322feSeschrock static uint64_t 20927f7322feSeschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 20937f7322feSeschrock { 20947f7322feSeschrock nvlist_t *nv; 20957f7322feSeschrock uint64_t value; 20967f7322feSeschrock 2097a2eea2e1Sahrens *source = NULL; 20987f7322feSeschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 20997f7322feSeschrock zfs_prop_to_name(prop), &nv) == 0) { 2100990b4856Slling verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0); 2101990b4856Slling (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 21027f7322feSeschrock } else { 21032e5e9e19SSanjeev Bagewadi verify(!zhp->zfs_props_table || 21042e5e9e19SSanjeev Bagewadi zhp->zfs_props_table[prop] == B_TRUE); 21057f7322feSeschrock value = zfs_prop_default_numeric(prop); 21067f7322feSeschrock *source = ""; 21077f7322feSeschrock } 21087f7322feSeschrock 21097f7322feSeschrock return (value); 21107f7322feSeschrock } 21117f7322feSeschrock 21129c3fd121SMatthew Ahrens static const char * 21137f7322feSeschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 21147f7322feSeschrock { 21157f7322feSeschrock nvlist_t *nv; 21169c3fd121SMatthew Ahrens const char *value; 21177f7322feSeschrock 2118a2eea2e1Sahrens *source = NULL; 21197f7322feSeschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 21207f7322feSeschrock zfs_prop_to_name(prop), &nv) == 0) { 21219c3fd121SMatthew Ahrens value = fnvlist_lookup_string(nv, ZPROP_VALUE); 2122990b4856Slling (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 21237f7322feSeschrock } else { 21242e5e9e19SSanjeev Bagewadi verify(!zhp->zfs_props_table || 21252e5e9e19SSanjeev Bagewadi zhp->zfs_props_table[prop] == B_TRUE); 21269c3fd121SMatthew Ahrens value = zfs_prop_default_string(prop); 21277f7322feSeschrock *source = ""; 21287f7322feSeschrock } 21297f7322feSeschrock 21307f7322feSeschrock return (value); 21317f7322feSeschrock } 21327f7322feSeschrock 213392241e0bSTom Erickson static boolean_t 213492241e0bSTom Erickson zfs_is_recvd_props_mode(zfs_handle_t *zhp) 213592241e0bSTom Erickson { 213692241e0bSTom Erickson return (zhp->zfs_props == zhp->zfs_recvd_props); 213792241e0bSTom Erickson } 213892241e0bSTom Erickson 213992241e0bSTom Erickson static void 214092241e0bSTom Erickson zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie) 214192241e0bSTom Erickson { 214292241e0bSTom Erickson *cookie = (uint64_t)(uintptr_t)zhp->zfs_props; 214392241e0bSTom Erickson zhp->zfs_props = zhp->zfs_recvd_props; 214492241e0bSTom Erickson } 214592241e0bSTom Erickson 214692241e0bSTom Erickson static void 214792241e0bSTom Erickson zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie) 214892241e0bSTom Erickson { 214992241e0bSTom Erickson zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie; 215092241e0bSTom Erickson *cookie = 0; 215192241e0bSTom Erickson } 215292241e0bSTom Erickson 21537f7322feSeschrock /* 2154fa9e4066Sahrens * Internal function for getting a numeric property. Both zfs_prop_get() and 2155fa9e4066Sahrens * zfs_prop_get_int() are built using this interface. 2156fa9e4066Sahrens * 2157fa9e4066Sahrens * Certain properties can be overridden using 'mount -o'. In this case, scan 2158fa9e4066Sahrens * the contents of the /etc/mnttab entry, searching for the appropriate options. 2159fa9e4066Sahrens * If they differ from the on-disk values, report the current values and mark 2160fa9e4066Sahrens * the source "temporary". 2161fa9e4066Sahrens */ 216299653d4eSeschrock static int 2163990b4856Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src, 216499653d4eSeschrock char **source, uint64_t *val) 2165fa9e4066Sahrens { 2166bd00f61bSrm160521 zfs_cmd_t zc = { 0 }; 216796510749Stimh nvlist_t *zplprops = NULL; 2168fa9e4066Sahrens struct mnttab mnt; 21693ccfa83cSahrens char *mntopt_on = NULL; 21703ccfa83cSahrens char *mntopt_off = NULL; 217192241e0bSTom Erickson boolean_t received = zfs_is_recvd_props_mode(zhp); 2172fa9e4066Sahrens 2173fa9e4066Sahrens *source = NULL; 2174fa9e4066Sahrens 21753ccfa83cSahrens switch (prop) { 21763ccfa83cSahrens case ZFS_PROP_ATIME: 21773ccfa83cSahrens mntopt_on = MNTOPT_ATIME; 21783ccfa83cSahrens mntopt_off = MNTOPT_NOATIME; 21793ccfa83cSahrens break; 21803ccfa83cSahrens 21813ccfa83cSahrens case ZFS_PROP_DEVICES: 21823ccfa83cSahrens mntopt_on = MNTOPT_DEVICES; 21833ccfa83cSahrens mntopt_off = MNTOPT_NODEVICES; 21843ccfa83cSahrens break; 21853ccfa83cSahrens 21863ccfa83cSahrens case ZFS_PROP_EXEC: 21873ccfa83cSahrens mntopt_on = MNTOPT_EXEC; 21883ccfa83cSahrens mntopt_off = MNTOPT_NOEXEC; 21893ccfa83cSahrens break; 21903ccfa83cSahrens 21913ccfa83cSahrens case ZFS_PROP_READONLY: 21923ccfa83cSahrens mntopt_on = MNTOPT_RO; 21933ccfa83cSahrens mntopt_off = MNTOPT_RW; 21943ccfa83cSahrens break; 21953ccfa83cSahrens 21963ccfa83cSahrens case ZFS_PROP_SETUID: 21973ccfa83cSahrens mntopt_on = MNTOPT_SETUID; 21983ccfa83cSahrens mntopt_off = MNTOPT_NOSETUID; 21993ccfa83cSahrens break; 22003ccfa83cSahrens 22013ccfa83cSahrens case ZFS_PROP_XATTR: 22023ccfa83cSahrens mntopt_on = MNTOPT_XATTR; 22033ccfa83cSahrens mntopt_off = MNTOPT_NOXATTR; 22043ccfa83cSahrens break; 2205da6c28aaSamw 2206da6c28aaSamw case ZFS_PROP_NBMAND: 2207da6c28aaSamw mntopt_on = MNTOPT_NBMAND; 2208da6c28aaSamw mntopt_off = MNTOPT_NONBMAND; 2209da6c28aaSamw break; 221088f61deeSIgor Kozhukhov 221188f61deeSIgor Kozhukhov default: 221288f61deeSIgor Kozhukhov break; 22133ccfa83cSahrens } 22143ccfa83cSahrens 22153bb79becSeschrock /* 22163bb79becSeschrock * Because looking up the mount options is potentially expensive 22173bb79becSeschrock * (iterating over all of /etc/mnttab), we defer its calculation until 22183bb79becSeschrock * we're looking up a property which requires its presence. 22193bb79becSeschrock */ 22203bb79becSeschrock if (!zhp->zfs_mntcheck && 22213ccfa83cSahrens (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) { 2222ebedde84SEric Taylor libzfs_handle_t *hdl = zhp->zfs_hdl; 2223ebedde84SEric Taylor struct mnttab entry; 22243bb79becSeschrock 2225ebedde84SEric Taylor if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) { 2226ebedde84SEric Taylor zhp->zfs_mntopts = zfs_strdup(hdl, 22273ccfa83cSahrens entry.mnt_mntopts); 22283ccfa83cSahrens if (zhp->zfs_mntopts == NULL) 22293bb79becSeschrock return (-1); 22303ccfa83cSahrens } 22313bb79becSeschrock 22323bb79becSeschrock zhp->zfs_mntcheck = B_TRUE; 22333bb79becSeschrock } 22343bb79becSeschrock 2235fa9e4066Sahrens if (zhp->zfs_mntopts == NULL) 2236fa9e4066Sahrens mnt.mnt_mntopts = ""; 2237fa9e4066Sahrens else 2238fa9e4066Sahrens mnt.mnt_mntopts = zhp->zfs_mntopts; 2239fa9e4066Sahrens 2240fa9e4066Sahrens switch (prop) { 2241fa9e4066Sahrens case ZFS_PROP_ATIME: 2242fa9e4066Sahrens case ZFS_PROP_DEVICES: 2243fa9e4066Sahrens case ZFS_PROP_EXEC: 22443ccfa83cSahrens case ZFS_PROP_READONLY: 22453ccfa83cSahrens case ZFS_PROP_SETUID: 22463ccfa83cSahrens case ZFS_PROP_XATTR: 2247da6c28aaSamw case ZFS_PROP_NBMAND: 224899653d4eSeschrock *val = getprop_uint64(zhp, prop, source); 2249fa9e4066Sahrens 225092241e0bSTom Erickson if (received) 225192241e0bSTom Erickson break; 225292241e0bSTom Erickson 22533ccfa83cSahrens if (hasmntopt(&mnt, mntopt_on) && !*val) { 225499653d4eSeschrock *val = B_TRUE; 2255fa9e4066Sahrens if (src) 2256990b4856Slling *src = ZPROP_SRC_TEMPORARY; 22573ccfa83cSahrens } else if (hasmntopt(&mnt, mntopt_off) && *val) { 225899653d4eSeschrock *val = B_FALSE; 2259fa9e4066Sahrens if (src) 2260990b4856Slling *src = ZPROP_SRC_TEMPORARY; 2261fa9e4066Sahrens } 226299653d4eSeschrock break; 2263fa9e4066Sahrens 22643ccfa83cSahrens case ZFS_PROP_CANMOUNT: 2265d41c4376SMark J Musante case ZFS_PROP_VOLSIZE: 2266fa9e4066Sahrens case ZFS_PROP_QUOTA: 2267a9799022Sck153898 case ZFS_PROP_REFQUOTA: 2268fa9e4066Sahrens case ZFS_PROP_RESERVATION: 2269a9799022Sck153898 case ZFS_PROP_REFRESERVATION: 2270a2afb611SJerry Jelinek case ZFS_PROP_FILESYSTEM_LIMIT: 2271a2afb611SJerry Jelinek case ZFS_PROP_SNAPSHOT_LIMIT: 2272a2afb611SJerry Jelinek case ZFS_PROP_FILESYSTEM_COUNT: 2273a2afb611SJerry Jelinek case ZFS_PROP_SNAPSHOT_COUNT: 2274a2eea2e1Sahrens *val = getprop_uint64(zhp, prop, source); 227592241e0bSTom Erickson 227692241e0bSTom Erickson if (*source == NULL) { 227792241e0bSTom Erickson /* not default, must be local */ 2278fa9e4066Sahrens *source = zhp->zfs_name; 227992241e0bSTom Erickson } 228099653d4eSeschrock break; 2281fa9e4066Sahrens 2282fa9e4066Sahrens case ZFS_PROP_MOUNTED: 228399653d4eSeschrock *val = (zhp->zfs_mntopts != NULL); 228499653d4eSeschrock break; 2285fa9e4066Sahrens 228639c23413Seschrock case ZFS_PROP_NUMCLONES: 228739c23413Seschrock *val = zhp->zfs_dmustats.dds_num_clones; 228839c23413Seschrock break; 228939c23413Seschrock 2290bd00f61bSrm160521 case ZFS_PROP_VERSION: 2291de8267e0Stimh case ZFS_PROP_NORMALIZE: 2292de8267e0Stimh case ZFS_PROP_UTF8ONLY: 2293de8267e0Stimh case ZFS_PROP_CASE: 2294de8267e0Stimh if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) || 2295de8267e0Stimh zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 22963d7934e1Srm160521 return (-1); 2297bd00f61bSrm160521 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2298de8267e0Stimh if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) { 2299de8267e0Stimh zcmd_free_nvlists(&zc); 2300f4b94bdeSMatthew Ahrens return (-1); 2301bd00f61bSrm160521 } 2302de8267e0Stimh if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 || 2303de8267e0Stimh nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop), 2304de8267e0Stimh val) != 0) { 2305de8267e0Stimh zcmd_free_nvlists(&zc); 2306f4b94bdeSMatthew Ahrens return (-1); 2307de8267e0Stimh } 230896510749Stimh nvlist_free(zplprops); 2309de8267e0Stimh zcmd_free_nvlists(&zc); 2310bd00f61bSrm160521 break; 2311bd00f61bSrm160521 2312ca48f36fSKeith M Wesolowski case ZFS_PROP_INCONSISTENT: 2313ca48f36fSKeith M Wesolowski *val = zhp->zfs_dmustats.dds_inconsistent; 2314ca48f36fSKeith M Wesolowski break; 2315ca48f36fSKeith M Wesolowski 2316fa9e4066Sahrens default: 2317e7437265Sahrens switch (zfs_prop_get_type(prop)) { 231891ebeef5Sahrens case PROP_TYPE_NUMBER: 231991ebeef5Sahrens case PROP_TYPE_INDEX: 2320e7437265Sahrens *val = getprop_uint64(zhp, prop, source); 232174e7dc98SMatthew Ahrens /* 232214843421SMatthew Ahrens * If we tried to use a default value for a 232374e7dc98SMatthew Ahrens * readonly property, it means that it was not 23244d86c0eaSMatthew Ahrens * present. Note this only applies to "truly" 23254d86c0eaSMatthew Ahrens * readonly properties, not set-once properties 23264d86c0eaSMatthew Ahrens * like volblocksize. 232774e7dc98SMatthew Ahrens */ 232874e7dc98SMatthew Ahrens if (zfs_prop_readonly(prop) && 23294d86c0eaSMatthew Ahrens !zfs_prop_setonce(prop) && 233031f572c2STom Erickson *source != NULL && (*source)[0] == '\0') { 233131f572c2STom Erickson *source = NULL; 2332ad2760acSMatthew Ahrens return (-1); 233374e7dc98SMatthew Ahrens } 2334e7437265Sahrens break; 2335e7437265Sahrens 233691ebeef5Sahrens case PROP_TYPE_STRING: 2337e7437265Sahrens default: 233899653d4eSeschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 233999653d4eSeschrock "cannot get non-numeric property")); 234099653d4eSeschrock return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP, 234199653d4eSeschrock dgettext(TEXT_DOMAIN, "internal error"))); 2342fa9e4066Sahrens } 2343e7437265Sahrens } 2344fa9e4066Sahrens 2345fa9e4066Sahrens return (0); 2346fa9e4066Sahrens } 2347fa9e4066Sahrens 2348fa9e4066Sahrens /* 2349fa9e4066Sahrens * Calculate the source type, given the raw source string. 2350fa9e4066Sahrens */ 2351fa9e4066Sahrens static void 2352990b4856Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source, 2353fa9e4066Sahrens char *statbuf, size_t statlen) 2354fa9e4066Sahrens { 2355990b4856Slling if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY) 2356fa9e4066Sahrens return; 2357fa9e4066Sahrens 2358fa9e4066Sahrens if (source == NULL) { 2359990b4856Slling *srctype = ZPROP_SRC_NONE; 2360fa9e4066Sahrens } else if (source[0] == '\0') { 2361990b4856Slling *srctype = ZPROP_SRC_DEFAULT; 236292241e0bSTom Erickson } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) { 236392241e0bSTom Erickson *srctype = ZPROP_SRC_RECEIVED; 2364fa9e4066Sahrens } else { 2365fa9e4066Sahrens if (strcmp(source, zhp->zfs_name) == 0) { 2366990b4856Slling *srctype = ZPROP_SRC_LOCAL; 2367fa9e4066Sahrens } else { 2368fa9e4066Sahrens (void) strlcpy(statbuf, source, statlen); 2369990b4856Slling *srctype = ZPROP_SRC_INHERITED; 2370fa9e4066Sahrens } 2371fa9e4066Sahrens } 2372fa9e4066Sahrens 2373fa9e4066Sahrens } 2374fa9e4066Sahrens 237592241e0bSTom Erickson int 237692241e0bSTom Erickson zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf, 237792241e0bSTom Erickson size_t proplen, boolean_t literal) 237892241e0bSTom Erickson { 237992241e0bSTom Erickson zfs_prop_t prop; 238092241e0bSTom Erickson int err = 0; 238192241e0bSTom Erickson 238292241e0bSTom Erickson if (zhp->zfs_recvd_props == NULL) 238392241e0bSTom Erickson if (get_recvd_props_ioctl(zhp) != 0) 238492241e0bSTom Erickson return (-1); 238592241e0bSTom Erickson 238692241e0bSTom Erickson prop = zfs_name_to_prop(propname); 238792241e0bSTom Erickson 238892241e0bSTom Erickson if (prop != ZPROP_INVAL) { 238992241e0bSTom Erickson uint64_t cookie; 239092241e0bSTom Erickson if (!nvlist_exists(zhp->zfs_recvd_props, propname)) 239192241e0bSTom Erickson return (-1); 239292241e0bSTom Erickson zfs_set_recvd_props_mode(zhp, &cookie); 239392241e0bSTom Erickson err = zfs_prop_get(zhp, prop, propbuf, proplen, 239492241e0bSTom Erickson NULL, NULL, 0, literal); 239592241e0bSTom Erickson zfs_unset_recvd_props_mode(zhp, &cookie); 239692241e0bSTom Erickson } else { 239792241e0bSTom Erickson nvlist_t *propval; 239892241e0bSTom Erickson char *recvdval; 239992241e0bSTom Erickson if (nvlist_lookup_nvlist(zhp->zfs_recvd_props, 240092241e0bSTom Erickson propname, &propval) != 0) 240192241e0bSTom Erickson return (-1); 240292241e0bSTom Erickson verify(nvlist_lookup_string(propval, ZPROP_VALUE, 240392241e0bSTom Erickson &recvdval) == 0); 240492241e0bSTom Erickson (void) strlcpy(propbuf, recvdval, proplen); 240592241e0bSTom Erickson } 240692241e0bSTom Erickson 240792241e0bSTom Erickson return (err == 0 ? 0 : -1); 240892241e0bSTom Erickson } 240992241e0bSTom Erickson 241019b94df9SMatthew Ahrens static int 241119b94df9SMatthew Ahrens get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen) 241219b94df9SMatthew Ahrens { 241319b94df9SMatthew Ahrens nvlist_t *value; 241419b94df9SMatthew Ahrens nvpair_t *pair; 241519b94df9SMatthew Ahrens 241619b94df9SMatthew Ahrens value = zfs_get_clones_nvl(zhp); 2417*99ea293eSMatt Fiddaman if (value == NULL || nvlist_empty(value)) 241819b94df9SMatthew Ahrens return (-1); 241919b94df9SMatthew Ahrens 242019b94df9SMatthew Ahrens propbuf[0] = '\0'; 242119b94df9SMatthew Ahrens for (pair = nvlist_next_nvpair(value, NULL); pair != NULL; 242219b94df9SMatthew Ahrens pair = nvlist_next_nvpair(value, pair)) { 242319b94df9SMatthew Ahrens if (propbuf[0] != '\0') 242419b94df9SMatthew Ahrens (void) strlcat(propbuf, ",", proplen); 242519b94df9SMatthew Ahrens (void) strlcat(propbuf, nvpair_name(pair), proplen); 242619b94df9SMatthew Ahrens } 242719b94df9SMatthew Ahrens 242819b94df9SMatthew Ahrens return (0); 242919b94df9SMatthew Ahrens } 243019b94df9SMatthew Ahrens 243119b94df9SMatthew Ahrens struct get_clones_arg { 243219b94df9SMatthew Ahrens uint64_t numclones; 243319b94df9SMatthew Ahrens nvlist_t *value; 243419b94df9SMatthew Ahrens const char *origin; 24359adfa60dSMatthew Ahrens char buf[ZFS_MAX_DATASET_NAME_LEN]; 243619b94df9SMatthew Ahrens }; 243719b94df9SMatthew Ahrens 243819b94df9SMatthew Ahrens int 243919b94df9SMatthew Ahrens get_clones_cb(zfs_handle_t *zhp, void *arg) 244019b94df9SMatthew Ahrens { 244119b94df9SMatthew Ahrens struct get_clones_arg *gca = arg; 244219b94df9SMatthew Ahrens 244319b94df9SMatthew Ahrens if (gca->numclones == 0) { 244419b94df9SMatthew Ahrens zfs_close(zhp); 244519b94df9SMatthew Ahrens return (0); 244619b94df9SMatthew Ahrens } 244719b94df9SMatthew Ahrens 244819b94df9SMatthew Ahrens if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf), 244919b94df9SMatthew Ahrens NULL, NULL, 0, B_TRUE) != 0) 245019b94df9SMatthew Ahrens goto out; 245119b94df9SMatthew Ahrens if (strcmp(gca->buf, gca->origin) == 0) { 24523b2aab18SMatthew Ahrens fnvlist_add_boolean(gca->value, zfs_get_name(zhp)); 245319b94df9SMatthew Ahrens gca->numclones--; 245419b94df9SMatthew Ahrens } 245519b94df9SMatthew Ahrens 245619b94df9SMatthew Ahrens out: 245719b94df9SMatthew Ahrens (void) zfs_iter_children(zhp, get_clones_cb, gca); 245819b94df9SMatthew Ahrens zfs_close(zhp); 245919b94df9SMatthew Ahrens return (0); 246019b94df9SMatthew Ahrens } 246119b94df9SMatthew Ahrens 246219b94df9SMatthew Ahrens nvlist_t * 246319b94df9SMatthew Ahrens zfs_get_clones_nvl(zfs_handle_t *zhp) 246419b94df9SMatthew Ahrens { 246519b94df9SMatthew Ahrens nvlist_t *nv, *value; 246619b94df9SMatthew Ahrens 246719b94df9SMatthew Ahrens if (nvlist_lookup_nvlist(zhp->zfs_props, 246819b94df9SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) { 246919b94df9SMatthew Ahrens struct get_clones_arg gca; 247019b94df9SMatthew Ahrens 247119b94df9SMatthew Ahrens /* 247219b94df9SMatthew Ahrens * if this is a snapshot, then the kernel wasn't able 247319b94df9SMatthew Ahrens * to get the clones. Do it by slowly iterating. 247419b94df9SMatthew Ahrens */ 247519b94df9SMatthew Ahrens if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) 247619b94df9SMatthew Ahrens return (NULL); 247719b94df9SMatthew Ahrens if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0) 247819b94df9SMatthew Ahrens return (NULL); 247919b94df9SMatthew Ahrens if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) { 248019b94df9SMatthew Ahrens nvlist_free(nv); 248119b94df9SMatthew Ahrens return (NULL); 248219b94df9SMatthew Ahrens } 248319b94df9SMatthew Ahrens 248419b94df9SMatthew Ahrens gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES); 248519b94df9SMatthew Ahrens gca.value = value; 248619b94df9SMatthew Ahrens gca.origin = zhp->zfs_name; 248719b94df9SMatthew Ahrens 248819b94df9SMatthew Ahrens if (gca.numclones != 0) { 248919b94df9SMatthew Ahrens zfs_handle_t *root; 24909adfa60dSMatthew Ahrens char pool[ZFS_MAX_DATASET_NAME_LEN]; 249119b94df9SMatthew Ahrens char *cp = pool; 249219b94df9SMatthew Ahrens 249319b94df9SMatthew Ahrens /* get the pool name */ 249419b94df9SMatthew Ahrens (void) strlcpy(pool, zhp->zfs_name, sizeof (pool)); 249519b94df9SMatthew Ahrens (void) strsep(&cp, "/@"); 249619b94df9SMatthew Ahrens root = zfs_open(zhp->zfs_hdl, pool, 249719b94df9SMatthew Ahrens ZFS_TYPE_FILESYSTEM); 249819b94df9SMatthew Ahrens 249919b94df9SMatthew Ahrens (void) get_clones_cb(root, &gca); 250019b94df9SMatthew Ahrens } 250119b94df9SMatthew Ahrens 250219b94df9SMatthew Ahrens if (gca.numclones != 0 || 250319b94df9SMatthew Ahrens nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 || 250419b94df9SMatthew Ahrens nvlist_add_nvlist(zhp->zfs_props, 250519b94df9SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) { 250619b94df9SMatthew Ahrens nvlist_free(nv); 250719b94df9SMatthew Ahrens nvlist_free(value); 250819b94df9SMatthew Ahrens return (NULL); 250919b94df9SMatthew Ahrens } 251019b94df9SMatthew Ahrens nvlist_free(nv); 251119b94df9SMatthew Ahrens nvlist_free(value); 251219b94df9SMatthew Ahrens verify(0 == nvlist_lookup_nvlist(zhp->zfs_props, 251319b94df9SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_CLONES), &nv)); 251419b94df9SMatthew Ahrens } 251519b94df9SMatthew Ahrens 251619b94df9SMatthew Ahrens verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0); 251719b94df9SMatthew Ahrens 251819b94df9SMatthew Ahrens return (value); 251919b94df9SMatthew Ahrens } 252019b94df9SMatthew Ahrens 2521fa9e4066Sahrens /* 2522dfc11533SChris Williamson * Accepts a property and value and checks that the value 2523dfc11533SChris Williamson * matches the one found by the channel program. If they are 2524dfc11533SChris Williamson * not equal, print both of them. 2525dfc11533SChris Williamson */ 2526dfc11533SChris Williamson void 2527dfc11533SChris Williamson zcp_check(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t intval, 2528dfc11533SChris Williamson const char *strval) 2529dfc11533SChris Williamson { 2530dfc11533SChris Williamson if (!zhp->zfs_hdl->libzfs_prop_debug) 2531dfc11533SChris Williamson return; 2532dfc11533SChris Williamson int error; 2533dfc11533SChris Williamson char *poolname = zhp->zpool_hdl->zpool_name; 2534dfc11533SChris Williamson const char *program = 2535dfc11533SChris Williamson "args = ...\n" 2536dfc11533SChris Williamson "ds = args['dataset']\n" 2537dfc11533SChris Williamson "prop = args['property']\n" 2538dfc11533SChris Williamson "value, setpoint = zfs.get_prop(ds, prop)\n" 2539dfc11533SChris Williamson "return {value=value, setpoint=setpoint}\n"; 2540dfc11533SChris Williamson nvlist_t *outnvl; 2541dfc11533SChris Williamson nvlist_t *retnvl; 2542dfc11533SChris Williamson nvlist_t *argnvl = fnvlist_alloc(); 2543dfc11533SChris Williamson 2544dfc11533SChris Williamson fnvlist_add_string(argnvl, "dataset", zhp->zfs_name); 2545dfc11533SChris Williamson fnvlist_add_string(argnvl, "property", zfs_prop_to_name(prop)); 2546dfc11533SChris Williamson 2547a3b28680SSerapheim Dimitropoulos error = lzc_channel_program_nosync(poolname, program, 2548dfc11533SChris Williamson 10 * 1000 * 1000, 10 * 1024 * 1024, argnvl, &outnvl); 2549dfc11533SChris Williamson 2550dfc11533SChris Williamson if (error == 0) { 2551dfc11533SChris Williamson retnvl = fnvlist_lookup_nvlist(outnvl, "return"); 2552dfc11533SChris Williamson if (zfs_prop_get_type(prop) == PROP_TYPE_NUMBER) { 2553dfc11533SChris Williamson int64_t ans; 2554dfc11533SChris Williamson error = nvlist_lookup_int64(retnvl, "value", &ans); 2555dfc11533SChris Williamson if (error != 0) { 2556dfc11533SChris Williamson (void) fprintf(stderr, "zcp check error: %u\n", 2557dfc11533SChris Williamson error); 2558dfc11533SChris Williamson return; 2559dfc11533SChris Williamson } 2560dfc11533SChris Williamson if (ans != intval) { 2561dfc11533SChris Williamson (void) fprintf(stderr, 2562dfc11533SChris Williamson "%s: zfs found %lld, but zcp found %lld\n", 2563dfc11533SChris Williamson zfs_prop_to_name(prop), 2564dfc11533SChris Williamson (longlong_t)intval, (longlong_t)ans); 2565dfc11533SChris Williamson } 2566dfc11533SChris Williamson } else { 2567dfc11533SChris Williamson char *str_ans; 2568dfc11533SChris Williamson error = nvlist_lookup_string(retnvl, "value", &str_ans); 2569dfc11533SChris Williamson if (error != 0) { 2570dfc11533SChris Williamson (void) fprintf(stderr, "zcp check error: %u\n", 2571dfc11533SChris Williamson error); 2572dfc11533SChris Williamson return; 2573dfc11533SChris Williamson } 2574dfc11533SChris Williamson if (strcmp(strval, str_ans) != 0) { 2575dfc11533SChris Williamson (void) fprintf(stderr, 2576dfc11533SChris Williamson "%s: zfs found %s, but zcp found %s\n", 2577dfc11533SChris Williamson zfs_prop_to_name(prop), 2578dfc11533SChris Williamson strval, str_ans); 2579dfc11533SChris Williamson } 2580dfc11533SChris Williamson } 2581dfc11533SChris Williamson } else { 2582dfc11533SChris Williamson (void) fprintf(stderr, 2583dfc11533SChris Williamson "zcp check failed, channel program error: %u\n", error); 2584dfc11533SChris Williamson } 2585dfc11533SChris Williamson nvlist_free(argnvl); 2586dfc11533SChris Williamson nvlist_free(outnvl); 2587dfc11533SChris Williamson } 2588dfc11533SChris Williamson 2589dfc11533SChris Williamson /* 2590fa9e4066Sahrens * Retrieve a property from the given object. If 'literal' is specified, then 2591fa9e4066Sahrens * numbers are left as exact values. Otherwise, numbers are converted to a 2592fa9e4066Sahrens * human-readable form. 2593fa9e4066Sahrens * 2594fa9e4066Sahrens * Returns 0 on success, or -1 on error. 2595fa9e4066Sahrens */ 2596fa9e4066Sahrens int 2597fa9e4066Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen, 2598990b4856Slling zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal) 2599fa9e4066Sahrens { 2600fa9e4066Sahrens char *source = NULL; 2601fa9e4066Sahrens uint64_t val; 26029c3fd121SMatthew Ahrens const char *str; 2603e9dbad6fSeschrock const char *strval; 260492241e0bSTom Erickson boolean_t received = zfs_is_recvd_props_mode(zhp); 2605fa9e4066Sahrens 2606fa9e4066Sahrens /* 2607fa9e4066Sahrens * Check to see if this property applies to our object 2608fa9e4066Sahrens */ 2609fa9e4066Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 2610fa9e4066Sahrens return (-1); 2611fa9e4066Sahrens 261292241e0bSTom Erickson if (received && zfs_prop_readonly(prop)) 261392241e0bSTom Erickson return (-1); 261492241e0bSTom Erickson 2615fa9e4066Sahrens if (src) 2616990b4856Slling *src = ZPROP_SRC_NONE; 2617fa9e4066Sahrens 2618fa9e4066Sahrens switch (prop) { 2619fa9e4066Sahrens case ZFS_PROP_CREATION: 2620fa9e4066Sahrens /* 2621fa9e4066Sahrens * 'creation' is a time_t stored in the statistics. We convert 2622fa9e4066Sahrens * this into a string unless 'literal' is specified. 2623fa9e4066Sahrens */ 2624fa9e4066Sahrens { 2625a2eea2e1Sahrens val = getprop_uint64(zhp, prop, &source); 2626a2eea2e1Sahrens time_t time = (time_t)val; 2627fa9e4066Sahrens struct tm t; 2628fa9e4066Sahrens 2629fa9e4066Sahrens if (literal || 2630fa9e4066Sahrens localtime_r(&time, &t) == NULL || 2631fa9e4066Sahrens strftime(propbuf, proplen, "%a %b %e %k:%M %Y", 2632fa9e4066Sahrens &t) == 0) 2633a2eea2e1Sahrens (void) snprintf(propbuf, proplen, "%llu", val); 2634fa9e4066Sahrens } 2635dfc11533SChris Williamson zcp_check(zhp, prop, val, NULL); 2636fa9e4066Sahrens break; 2637fa9e4066Sahrens 2638fa9e4066Sahrens case ZFS_PROP_MOUNTPOINT: 2639fa9e4066Sahrens /* 2640fa9e4066Sahrens * Getting the precise mountpoint can be tricky. 2641fa9e4066Sahrens * 2642fa9e4066Sahrens * - for 'none' or 'legacy', return those values. 2643fa9e4066Sahrens * - for inherited mountpoints, we want to take everything 2644fa9e4066Sahrens * after our ancestor and append it to the inherited value. 2645fa9e4066Sahrens * 2646fa9e4066Sahrens * If the pool has an alternate root, we want to prepend that 2647fa9e4066Sahrens * root to any values we return. 2648fa9e4066Sahrens */ 264929ab75c9Srm160521 26507f7322feSeschrock str = getprop_string(zhp, prop, &source); 2651fa9e4066Sahrens 2652b21718f0Sgw25295 if (str[0] == '/') { 265329ab75c9Srm160521 char buf[MAXPATHLEN]; 265429ab75c9Srm160521 char *root = buf; 2655a79992aaSTom Erickson const char *relpath; 2656fa9e4066Sahrens 2657a79992aaSTom Erickson /* 2658a79992aaSTom Erickson * If we inherit the mountpoint, even from a dataset 2659a79992aaSTom Erickson * with a received value, the source will be the path of 2660a79992aaSTom Erickson * the dataset we inherit from. If source is 2661a79992aaSTom Erickson * ZPROP_SOURCE_VAL_RECVD, the received value is not 2662a79992aaSTom Erickson * inherited. 2663a79992aaSTom Erickson */ 2664a79992aaSTom Erickson if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) { 2665a79992aaSTom Erickson relpath = ""; 2666a79992aaSTom Erickson } else { 2667a79992aaSTom Erickson relpath = zhp->zfs_name + strlen(source); 2668fa9e4066Sahrens if (relpath[0] == '/') 2669fa9e4066Sahrens relpath++; 2670a79992aaSTom Erickson } 2671b21718f0Sgw25295 267229ab75c9Srm160521 if ((zpool_get_prop(zhp->zpool_hdl, 2673c58b3526SAdam Stevko ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL, 2674c58b3526SAdam Stevko B_FALSE)) || (strcmp(root, "-") == 0)) 267529ab75c9Srm160521 root[0] = '\0'; 2676b21718f0Sgw25295 /* 2677b21718f0Sgw25295 * Special case an alternate root of '/'. This will 2678b21718f0Sgw25295 * avoid having multiple leading slashes in the 2679b21718f0Sgw25295 * mountpoint path. 2680b21718f0Sgw25295 */ 2681b21718f0Sgw25295 if (strcmp(root, "/") == 0) 2682b21718f0Sgw25295 root++; 2683b21718f0Sgw25295 2684b21718f0Sgw25295 /* 2685b21718f0Sgw25295 * If the mountpoint is '/' then skip over this 2686b21718f0Sgw25295 * if we are obtaining either an alternate root or 2687b21718f0Sgw25295 * an inherited mountpoint. 2688b21718f0Sgw25295 */ 2689b21718f0Sgw25295 if (str[1] == '\0' && (root[0] != '\0' || 2690b21718f0Sgw25295 relpath[0] != '\0')) 26917f7322feSeschrock str++; 2692fa9e4066Sahrens 2693fa9e4066Sahrens if (relpath[0] == '\0') 2694fa9e4066Sahrens (void) snprintf(propbuf, proplen, "%s%s", 26957f7322feSeschrock root, str); 2696fa9e4066Sahrens else 2697fa9e4066Sahrens (void) snprintf(propbuf, proplen, "%s%s%s%s", 26987f7322feSeschrock root, str, relpath[0] == '@' ? "" : "/", 2699fa9e4066Sahrens relpath); 2700fa9e4066Sahrens } else { 2701fa9e4066Sahrens /* 'legacy' or 'none' */ 27027f7322feSeschrock (void) strlcpy(propbuf, str, proplen); 2703fa9e4066Sahrens } 27045b8cbb8eSToomas Soome zcp_check(zhp, prop, 0, propbuf); 2705fa9e4066Sahrens break; 2706fa9e4066Sahrens 2707fa9e4066Sahrens case ZFS_PROP_ORIGIN: 27089c3fd121SMatthew Ahrens str = getprop_string(zhp, prop, &source); 27099c3fd121SMatthew Ahrens if (str == NULL) 2710fa9e4066Sahrens return (-1); 27119c3fd121SMatthew Ahrens (void) strlcpy(propbuf, str, proplen); 27125b8cbb8eSToomas Soome zcp_check(zhp, prop, 0, str); 2713fa9e4066Sahrens break; 2714fa9e4066Sahrens 271519b94df9SMatthew Ahrens case ZFS_PROP_CLONES: 271619b94df9SMatthew Ahrens if (get_clones_string(zhp, propbuf, proplen) != 0) 271719b94df9SMatthew Ahrens return (-1); 271819b94df9SMatthew Ahrens break; 271919b94df9SMatthew Ahrens 2720fa9e4066Sahrens case ZFS_PROP_QUOTA: 2721a9799022Sck153898 case ZFS_PROP_REFQUOTA: 2722fa9e4066Sahrens case ZFS_PROP_RESERVATION: 2723a9799022Sck153898 case ZFS_PROP_REFRESERVATION: 2724a9799022Sck153898 272599653d4eSeschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 272699653d4eSeschrock return (-1); 2727fa9e4066Sahrens /* 2728fa9e4066Sahrens * If quota or reservation is 0, we translate this into 'none' 2729fa9e4066Sahrens * (unless literal is set), and indicate that it's the default 2730fa9e4066Sahrens * value. Otherwise, we print the number nicely and indicate 2731fa9e4066Sahrens * that its set locally. 2732fa9e4066Sahrens */ 2733fa9e4066Sahrens if (val == 0) { 2734fa9e4066Sahrens if (literal) 2735fa9e4066Sahrens (void) strlcpy(propbuf, "0", proplen); 2736fa9e4066Sahrens else 2737fa9e4066Sahrens (void) strlcpy(propbuf, "none", proplen); 2738fa9e4066Sahrens } else { 2739fa9e4066Sahrens if (literal) 27405ad82045Snd150628 (void) snprintf(propbuf, proplen, "%llu", 27415ad82045Snd150628 (u_longlong_t)val); 2742fa9e4066Sahrens else 2743fa9e4066Sahrens zfs_nicenum(val, propbuf, proplen); 2744fa9e4066Sahrens } 2745dfc11533SChris Williamson zcp_check(zhp, prop, val, NULL); 2746fa9e4066Sahrens break; 2747fa9e4066Sahrens 2748a2afb611SJerry Jelinek case ZFS_PROP_FILESYSTEM_LIMIT: 2749a2afb611SJerry Jelinek case ZFS_PROP_SNAPSHOT_LIMIT: 2750a2afb611SJerry Jelinek case ZFS_PROP_FILESYSTEM_COUNT: 2751a2afb611SJerry Jelinek case ZFS_PROP_SNAPSHOT_COUNT: 2752a2afb611SJerry Jelinek 2753a2afb611SJerry Jelinek if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 2754a2afb611SJerry Jelinek return (-1); 2755a2afb611SJerry Jelinek 2756a2afb611SJerry Jelinek /* 2757a2afb611SJerry Jelinek * If limit is UINT64_MAX, we translate this into 'none' (unless 2758a2afb611SJerry Jelinek * literal is set), and indicate that it's the default value. 2759a2afb611SJerry Jelinek * Otherwise, we print the number nicely and indicate that it's 2760a2afb611SJerry Jelinek * set locally. 2761a2afb611SJerry Jelinek */ 2762a2afb611SJerry Jelinek if (literal) { 2763a2afb611SJerry Jelinek (void) snprintf(propbuf, proplen, "%llu", 2764a2afb611SJerry Jelinek (u_longlong_t)val); 2765a2afb611SJerry Jelinek } else if (val == UINT64_MAX) { 2766a2afb611SJerry Jelinek (void) strlcpy(propbuf, "none", proplen); 2767a2afb611SJerry Jelinek } else { 2768a2afb611SJerry Jelinek zfs_nicenum(val, propbuf, proplen); 2769a2afb611SJerry Jelinek } 2770dfc11533SChris Williamson 2771dfc11533SChris Williamson zcp_check(zhp, prop, val, NULL); 2772a2afb611SJerry Jelinek break; 2773a2afb611SJerry Jelinek 2774187d6ac0SMatt Ahrens case ZFS_PROP_REFRATIO: 2775fa9e4066Sahrens case ZFS_PROP_COMPRESSRATIO: 277699653d4eSeschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 277799653d4eSeschrock return (-1); 2778b24ab676SJeff Bonwick (void) snprintf(propbuf, proplen, "%llu.%02llux", 2779b24ab676SJeff Bonwick (u_longlong_t)(val / 100), 2780b24ab676SJeff Bonwick (u_longlong_t)(val % 100)); 2781dfc11533SChris Williamson zcp_check(zhp, prop, val, NULL); 2782fa9e4066Sahrens break; 2783fa9e4066Sahrens 2784fa9e4066Sahrens case ZFS_PROP_TYPE: 2785fa9e4066Sahrens switch (zhp->zfs_type) { 2786fa9e4066Sahrens case ZFS_TYPE_FILESYSTEM: 2787fa9e4066Sahrens str = "filesystem"; 2788fa9e4066Sahrens break; 2789fa9e4066Sahrens case ZFS_TYPE_VOLUME: 2790fa9e4066Sahrens str = "volume"; 2791fa9e4066Sahrens break; 2792fa9e4066Sahrens case ZFS_TYPE_SNAPSHOT: 2793fa9e4066Sahrens str = "snapshot"; 2794fa9e4066Sahrens break; 279578f17100SMatthew Ahrens case ZFS_TYPE_BOOKMARK: 279678f17100SMatthew Ahrens str = "bookmark"; 279778f17100SMatthew Ahrens break; 2798fa9e4066Sahrens default: 279999653d4eSeschrock abort(); 2800fa9e4066Sahrens } 2801fa9e4066Sahrens (void) snprintf(propbuf, proplen, "%s", str); 28025b8cbb8eSToomas Soome zcp_check(zhp, prop, 0, propbuf); 2803fa9e4066Sahrens break; 2804fa9e4066Sahrens 2805fa9e4066Sahrens case ZFS_PROP_MOUNTED: 2806fa9e4066Sahrens /* 2807fa9e4066Sahrens * The 'mounted' property is a pseudo-property that described 2808fa9e4066Sahrens * whether the filesystem is currently mounted. Even though 2809fa9e4066Sahrens * it's a boolean value, the typical values of "on" and "off" 2810fa9e4066Sahrens * don't make sense, so we translate to "yes" and "no". 2811fa9e4066Sahrens */ 281299653d4eSeschrock if (get_numeric_property(zhp, ZFS_PROP_MOUNTED, 281399653d4eSeschrock src, &source, &val) != 0) 281499653d4eSeschrock return (-1); 281599653d4eSeschrock if (val) 2816fa9e4066Sahrens (void) strlcpy(propbuf, "yes", proplen); 2817fa9e4066Sahrens else 2818fa9e4066Sahrens (void) strlcpy(propbuf, "no", proplen); 2819fa9e4066Sahrens break; 2820fa9e4066Sahrens 2821fa9e4066Sahrens case ZFS_PROP_NAME: 2822fa9e4066Sahrens /* 2823fa9e4066Sahrens * The 'name' property is a pseudo-property derived from the 2824fa9e4066Sahrens * dataset name. It is presented as a real property to simplify 2825fa9e4066Sahrens * consumers. 2826fa9e4066Sahrens */ 2827fa9e4066Sahrens (void) strlcpy(propbuf, zhp->zfs_name, proplen); 28285b8cbb8eSToomas Soome zcp_check(zhp, prop, 0, propbuf); 2829fa9e4066Sahrens break; 2830fa9e4066Sahrens 28314201a95eSRic Aleshire case ZFS_PROP_MLSLABEL: 28324201a95eSRic Aleshire { 28334201a95eSRic Aleshire m_label_t *new_sl = NULL; 28344201a95eSRic Aleshire char *ascii = NULL; /* human readable label */ 28354201a95eSRic Aleshire 28364201a95eSRic Aleshire (void) strlcpy(propbuf, 28374201a95eSRic Aleshire getprop_string(zhp, prop, &source), proplen); 28384201a95eSRic Aleshire 28394201a95eSRic Aleshire if (literal || (strcasecmp(propbuf, 28404201a95eSRic Aleshire ZFS_MLSLABEL_DEFAULT) == 0)) 28414201a95eSRic Aleshire break; 28424201a95eSRic Aleshire 28434201a95eSRic Aleshire /* 28444201a95eSRic Aleshire * Try to translate the internal hex string to 28454201a95eSRic Aleshire * human-readable output. If there are any 28464201a95eSRic Aleshire * problems just use the hex string. 28474201a95eSRic Aleshire */ 28484201a95eSRic Aleshire 28494201a95eSRic Aleshire if (str_to_label(propbuf, &new_sl, MAC_LABEL, 28504201a95eSRic Aleshire L_NO_CORRECTION, NULL) == -1) { 28514201a95eSRic Aleshire m_label_free(new_sl); 28524201a95eSRic Aleshire break; 28534201a95eSRic Aleshire } 28544201a95eSRic Aleshire 28554201a95eSRic Aleshire if (label_to_str(new_sl, &ascii, M_LABEL, 28564201a95eSRic Aleshire DEF_NAMES) != 0) { 28574201a95eSRic Aleshire if (ascii) 28584201a95eSRic Aleshire free(ascii); 28594201a95eSRic Aleshire m_label_free(new_sl); 28604201a95eSRic Aleshire break; 28614201a95eSRic Aleshire } 28624201a95eSRic Aleshire m_label_free(new_sl); 28634201a95eSRic Aleshire 28644201a95eSRic Aleshire (void) strlcpy(propbuf, ascii, proplen); 28654201a95eSRic Aleshire free(ascii); 28664201a95eSRic Aleshire } 28674201a95eSRic Aleshire break; 28684201a95eSRic Aleshire 2869f0f3ef5aSGarrett D'Amore case ZFS_PROP_GUID: 2870e8d4a73cSJosh Paetzel case ZFS_PROP_CREATETXG: 2871f0f3ef5aSGarrett D'Amore /* 2872f0f3ef5aSGarrett D'Amore * GUIDs are stored as numbers, but they are identifiers. 2873f0f3ef5aSGarrett D'Amore * We don't want them to be pretty printed, because pretty 2874f0f3ef5aSGarrett D'Amore * printing mangles the ID into a truncated and useless value. 2875f0f3ef5aSGarrett D'Amore */ 2876f0f3ef5aSGarrett D'Amore if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 2877f0f3ef5aSGarrett D'Amore return (-1); 2878f0f3ef5aSGarrett D'Amore (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val); 2879dfc11533SChris Williamson zcp_check(zhp, prop, val, NULL); 2880f0f3ef5aSGarrett D'Amore break; 2881f0f3ef5aSGarrett D'Amore 2882fa9e4066Sahrens default: 2883e7437265Sahrens switch (zfs_prop_get_type(prop)) { 288491ebeef5Sahrens case PROP_TYPE_NUMBER: 2885e7437265Sahrens if (get_numeric_property(zhp, prop, src, 2886dfc11533SChris Williamson &source, &val) != 0) { 2887e7437265Sahrens return (-1); 2888dfc11533SChris Williamson } 2889dfc11533SChris Williamson 2890dfc11533SChris Williamson if (literal) { 2891e7437265Sahrens (void) snprintf(propbuf, proplen, "%llu", 2892e7437265Sahrens (u_longlong_t)val); 2893dfc11533SChris Williamson } else { 2894e7437265Sahrens zfs_nicenum(val, propbuf, proplen); 2895dfc11533SChris Williamson } 2896dfc11533SChris Williamson zcp_check(zhp, prop, val, NULL); 2897e7437265Sahrens break; 2898e7437265Sahrens 289991ebeef5Sahrens case PROP_TYPE_STRING: 29009c3fd121SMatthew Ahrens str = getprop_string(zhp, prop, &source); 29019c3fd121SMatthew Ahrens if (str == NULL) 29029c3fd121SMatthew Ahrens return (-1); 2903dfc11533SChris Williamson 29049c3fd121SMatthew Ahrens (void) strlcpy(propbuf, str, proplen); 29055b8cbb8eSToomas Soome zcp_check(zhp, prop, 0, str); 2906e7437265Sahrens break; 2907e7437265Sahrens 290891ebeef5Sahrens case PROP_TYPE_INDEX: 2909fe192f76Sahrens if (get_numeric_property(zhp, prop, src, 2910fe192f76Sahrens &source, &val) != 0) 2911fe192f76Sahrens return (-1); 2912fe192f76Sahrens if (zfs_prop_index_to_string(prop, val, &strval) != 0) 2913e7437265Sahrens return (-1); 2914dfc11533SChris Williamson 2915e7437265Sahrens (void) strlcpy(propbuf, strval, proplen); 29165b8cbb8eSToomas Soome zcp_check(zhp, prop, 0, strval); 2917e7437265Sahrens break; 2918e7437265Sahrens 2919e7437265Sahrens default: 292099653d4eSeschrock abort(); 2921fa9e4066Sahrens } 2922e7437265Sahrens } 2923fa9e4066Sahrens 2924fa9e4066Sahrens get_source(zhp, src, source, statbuf, statlen); 2925fa9e4066Sahrens 2926fa9e4066Sahrens return (0); 2927fa9e4066Sahrens } 2928fa9e4066Sahrens 2929fa9e4066Sahrens /* 2930fa9e4066Sahrens * Utility function to get the given numeric property. Does no validation that 2931fa9e4066Sahrens * the given property is the appropriate type; should only be used with 2932fa9e4066Sahrens * hard-coded property types. 2933fa9e4066Sahrens */ 2934fa9e4066Sahrens uint64_t 2935fa9e4066Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop) 2936fa9e4066Sahrens { 2937fa9e4066Sahrens char *source; 293899653d4eSeschrock uint64_t val; 2939fa9e4066Sahrens 29403cb34c60Sahrens (void) get_numeric_property(zhp, prop, NULL, &source, &val); 294199653d4eSeschrock 294299653d4eSeschrock return (val); 2943fa9e4066Sahrens } 2944fa9e4066Sahrens 29457b97dc1aSrm160521 int 29467b97dc1aSrm160521 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val) 29477b97dc1aSrm160521 { 29487b97dc1aSrm160521 char buf[64]; 29497b97dc1aSrm160521 295014843421SMatthew Ahrens (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val); 29517b97dc1aSrm160521 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf)); 29527b97dc1aSrm160521 } 29537b97dc1aSrm160521 2954fa9e4066Sahrens /* 2955fa9e4066Sahrens * Similar to zfs_prop_get(), but returns the value as an integer. 2956fa9e4066Sahrens */ 2957fa9e4066Sahrens int 2958fa9e4066Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value, 2959990b4856Slling zprop_source_t *src, char *statbuf, size_t statlen) 2960fa9e4066Sahrens { 2961fa9e4066Sahrens char *source; 2962fa9e4066Sahrens 2963fa9e4066Sahrens /* 2964fa9e4066Sahrens * Check to see if this property applies to our object 2965fa9e4066Sahrens */ 2966e45ce728Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) { 2967ece3d9b3Slling return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE, 296899653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot get property '%s'"), 296999653d4eSeschrock zfs_prop_to_name(prop))); 2970e45ce728Sahrens } 2971fa9e4066Sahrens 2972fa9e4066Sahrens if (src) 2973990b4856Slling *src = ZPROP_SRC_NONE; 2974fa9e4066Sahrens 297599653d4eSeschrock if (get_numeric_property(zhp, prop, src, &source, value) != 0) 297699653d4eSeschrock return (-1); 2977fa9e4066Sahrens 2978fa9e4066Sahrens get_source(zhp, src, source, statbuf, statlen); 2979fa9e4066Sahrens 2980fa9e4066Sahrens return (0); 2981fa9e4066Sahrens } 2982fa9e4066Sahrens 298314843421SMatthew Ahrens static int 298414843421SMatthew Ahrens idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser, 298514843421SMatthew Ahrens char **domainp, idmap_rid_t *ridp) 298614843421SMatthew Ahrens { 298714843421SMatthew Ahrens idmap_get_handle_t *get_hdl = NULL; 298814843421SMatthew Ahrens idmap_stat status; 298914843421SMatthew Ahrens int err = EINVAL; 299014843421SMatthew Ahrens 29911fdeec65Sjoyce mcintosh if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS) 299214843421SMatthew Ahrens goto out; 299314843421SMatthew Ahrens 299414843421SMatthew Ahrens if (isuser) { 299514843421SMatthew Ahrens err = idmap_get_sidbyuid(get_hdl, id, 299614843421SMatthew Ahrens IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status); 299714843421SMatthew Ahrens } else { 299814843421SMatthew Ahrens err = idmap_get_sidbygid(get_hdl, id, 299914843421SMatthew Ahrens IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status); 300014843421SMatthew Ahrens } 300114843421SMatthew Ahrens if (err == IDMAP_SUCCESS && 300214843421SMatthew Ahrens idmap_get_mappings(get_hdl) == IDMAP_SUCCESS && 300314843421SMatthew Ahrens status == IDMAP_SUCCESS) 300414843421SMatthew Ahrens err = 0; 300514843421SMatthew Ahrens else 300614843421SMatthew Ahrens err = EINVAL; 300714843421SMatthew Ahrens out: 300814843421SMatthew Ahrens if (get_hdl) 300914843421SMatthew Ahrens idmap_get_destroy(get_hdl); 301014843421SMatthew Ahrens return (err); 301114843421SMatthew Ahrens } 301214843421SMatthew Ahrens 301314843421SMatthew Ahrens /* 301414843421SMatthew Ahrens * convert the propname into parameters needed by kernel 301514843421SMatthew Ahrens * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829 301614843421SMatthew Ahrens * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789 3017f67950b2SNasf-Fan * Eg: groupquota@staff -> ZFS_PROP_GROUPQUOTA, "", 1234 3018f67950b2SNasf-Fan * Eg: groupused@staff -> ZFS_PROP_GROUPUSED, "", 1234 3019f67950b2SNasf-Fan * Eg: projectquota@123 -> ZFS_PROP_PROJECTQUOTA, "", 123 3020f67950b2SNasf-Fan * Eg: projectused@789 -> ZFS_PROP_PROJECTUSED, "", 789 302114843421SMatthew Ahrens */ 302214843421SMatthew Ahrens static int 302314843421SMatthew Ahrens userquota_propname_decode(const char *propname, boolean_t zoned, 302414843421SMatthew Ahrens zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp) 302514843421SMatthew Ahrens { 302614843421SMatthew Ahrens zfs_userquota_prop_t type; 3027f67950b2SNasf-Fan char *cp; 302814843421SMatthew Ahrens boolean_t isuser; 3029f67950b2SNasf-Fan boolean_t isgroup; 3030f67950b2SNasf-Fan boolean_t isproject; 3031f67950b2SNasf-Fan struct passwd *pw; 3032f67950b2SNasf-Fan struct group *gr; 303314843421SMatthew Ahrens 303414843421SMatthew Ahrens domain[0] = '\0'; 3035f67950b2SNasf-Fan 3036f67950b2SNasf-Fan /* Figure out the property type ({user|group|project}{quota|space}) */ 303714843421SMatthew Ahrens for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) { 303814843421SMatthew Ahrens if (strncmp(propname, zfs_userquota_prop_prefixes[type], 303914843421SMatthew Ahrens strlen(zfs_userquota_prop_prefixes[type])) == 0) 304014843421SMatthew Ahrens break; 304114843421SMatthew Ahrens } 304214843421SMatthew Ahrens if (type == ZFS_NUM_USERQUOTA_PROPS) 304314843421SMatthew Ahrens return (EINVAL); 304414843421SMatthew Ahrens *typep = type; 304514843421SMatthew Ahrens 3046f67950b2SNasf-Fan isuser = (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_USERUSED || 3047f67950b2SNasf-Fan type == ZFS_PROP_USEROBJQUOTA || 3048f67950b2SNasf-Fan type == ZFS_PROP_USEROBJUSED); 3049f67950b2SNasf-Fan isgroup = (type == ZFS_PROP_GROUPQUOTA || type == ZFS_PROP_GROUPUSED || 3050f67950b2SNasf-Fan type == ZFS_PROP_GROUPOBJQUOTA || 3051f67950b2SNasf-Fan type == ZFS_PROP_GROUPOBJUSED); 3052f67950b2SNasf-Fan isproject = (type == ZFS_PROP_PROJECTQUOTA || 3053f67950b2SNasf-Fan type == ZFS_PROP_PROJECTUSED || type == ZFS_PROP_PROJECTOBJQUOTA || 3054f67950b2SNasf-Fan type == ZFS_PROP_PROJECTOBJUSED); 305514843421SMatthew Ahrens 305614843421SMatthew Ahrens cp = strchr(propname, '@') + 1; 305714843421SMatthew Ahrens 3058f67950b2SNasf-Fan if (isuser && (pw = getpwnam(cp)) != NULL) { 3059f67950b2SNasf-Fan if (zoned && getzoneid() == GLOBAL_ZONEID) 3060f67950b2SNasf-Fan return (ENOENT); 3061f67950b2SNasf-Fan *ridp = pw->pw_uid; 3062f67950b2SNasf-Fan } else if (isgroup && (gr = getgrnam(cp)) != NULL) { 3063f67950b2SNasf-Fan if (zoned && getzoneid() == GLOBAL_ZONEID) 3064f67950b2SNasf-Fan return (ENOENT); 3065f67950b2SNasf-Fan *ridp = gr->gr_gid; 3066f67950b2SNasf-Fan } else if (!isproject && strchr(cp, '@')) { 306714843421SMatthew Ahrens /* 306814843421SMatthew Ahrens * It's a SID name (eg "user@domain") that needs to be 30693b12c289SMatthew Ahrens * turned into S-1-domainID-RID. 307014843421SMatthew Ahrens */ 3071f67950b2SNasf-Fan directory_error_t e; 3072f67950b2SNasf-Fan char *numericsid = NULL; 3073f67950b2SNasf-Fan char *end; 30741ed6b69aSGordon Ross 307514843421SMatthew Ahrens if (zoned && getzoneid() == GLOBAL_ZONEID) 307614843421SMatthew Ahrens return (ENOENT); 30773b12c289SMatthew Ahrens if (isuser) { 3078f67950b2SNasf-Fan e = directory_sid_from_user_name(NULL, 3079f67950b2SNasf-Fan cp, &numericsid); 30803b12c289SMatthew Ahrens } else { 3081f67950b2SNasf-Fan e = directory_sid_from_group_name(NULL, 3082f67950b2SNasf-Fan cp, &numericsid); 30833b12c289SMatthew Ahrens } 3084f67950b2SNasf-Fan if (e != NULL) { 3085f67950b2SNasf-Fan directory_error_free(e); 308614843421SMatthew Ahrens return (ENOENT); 30873b12c289SMatthew Ahrens } 30883b12c289SMatthew Ahrens if (numericsid == NULL) 308914843421SMatthew Ahrens return (ENOENT); 30903b12c289SMatthew Ahrens cp = numericsid; 30913b12c289SMatthew Ahrens (void) strlcpy(domain, cp, domainlen); 309214843421SMatthew Ahrens cp = strrchr(domain, '-'); 309314843421SMatthew Ahrens *cp = '\0'; 309414843421SMatthew Ahrens cp++; 3095f67950b2SNasf-Fan 3096f67950b2SNasf-Fan errno = 0; 309714843421SMatthew Ahrens *ridp = strtoull(cp, &end, 10); 30983b12c289SMatthew Ahrens free(numericsid); 3099f67950b2SNasf-Fan 3100777badbaSMatthew Ahrens if (errno != 0 || *end != '\0') 310114843421SMatthew Ahrens return (EINVAL); 310214843421SMatthew Ahrens } else { 3103f67950b2SNasf-Fan /* It's a user/group/project ID (eg "12345"). */ 3104f67950b2SNasf-Fan char *end; 310514843421SMatthew Ahrens uid_t id = strtoul(cp, &end, 10); 3106f67950b2SNasf-Fan if (*end != '\0') 3107f67950b2SNasf-Fan return (EINVAL); 3108f67950b2SNasf-Fan if (id > MAXUID && !isproject) { 3109f67950b2SNasf-Fan /* It's an ephemeral ID. */ 311014843421SMatthew Ahrens idmap_rid_t rid; 311114843421SMatthew Ahrens char *mapdomain; 311214843421SMatthew Ahrens 311314843421SMatthew Ahrens if (idmap_id_to_numeric_domain_rid(id, isuser, 311414843421SMatthew Ahrens &mapdomain, &rid) != 0) 311514843421SMatthew Ahrens return (ENOENT); 31163b12c289SMatthew Ahrens (void) strlcpy(domain, mapdomain, domainlen); 311714843421SMatthew Ahrens *ridp = rid; 311814843421SMatthew Ahrens } else { 311914843421SMatthew Ahrens *ridp = id; 312014843421SMatthew Ahrens } 312114843421SMatthew Ahrens } 312214843421SMatthew Ahrens 312314843421SMatthew Ahrens return (0); 312414843421SMatthew Ahrens } 312514843421SMatthew Ahrens 3126edea4b55SLin Ling static int 3127edea4b55SLin Ling zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname, 3128edea4b55SLin Ling uint64_t *propvalue, zfs_userquota_prop_t *typep) 312914843421SMatthew Ahrens { 313014843421SMatthew Ahrens int err; 313114843421SMatthew Ahrens zfs_cmd_t zc = { 0 }; 313214843421SMatthew Ahrens 313319b94df9SMatthew Ahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 313414843421SMatthew Ahrens 313514843421SMatthew Ahrens err = userquota_propname_decode(propname, 313614843421SMatthew Ahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED), 3137edea4b55SLin Ling typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid); 3138edea4b55SLin Ling zc.zc_objset_type = *typep; 313914843421SMatthew Ahrens if (err) 314014843421SMatthew Ahrens return (err); 314114843421SMatthew Ahrens 314214843421SMatthew Ahrens err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc); 314314843421SMatthew Ahrens if (err) 314414843421SMatthew Ahrens return (err); 314514843421SMatthew Ahrens 3146edea4b55SLin Ling *propvalue = zc.zc_cookie; 3147edea4b55SLin Ling return (0); 3148edea4b55SLin Ling } 3149edea4b55SLin Ling 3150edea4b55SLin Ling int 3151edea4b55SLin Ling zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname, 3152edea4b55SLin Ling uint64_t *propvalue) 3153edea4b55SLin Ling { 3154edea4b55SLin Ling zfs_userquota_prop_t type; 3155edea4b55SLin Ling 3156edea4b55SLin Ling return (zfs_prop_get_userquota_common(zhp, propname, propvalue, 3157edea4b55SLin Ling &type)); 3158edea4b55SLin Ling } 3159edea4b55SLin Ling 3160edea4b55SLin Ling int 3161edea4b55SLin Ling zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname, 3162edea4b55SLin Ling char *propbuf, int proplen, boolean_t literal) 3163edea4b55SLin Ling { 3164edea4b55SLin Ling int err; 3165edea4b55SLin Ling uint64_t propvalue; 3166edea4b55SLin Ling zfs_userquota_prop_t type; 3167edea4b55SLin Ling 3168edea4b55SLin Ling err = zfs_prop_get_userquota_common(zhp, propname, &propvalue, 3169edea4b55SLin Ling &type); 3170edea4b55SLin Ling 3171edea4b55SLin Ling if (err) 3172edea4b55SLin Ling return (err); 3173edea4b55SLin Ling 317414843421SMatthew Ahrens if (literal) { 3175edea4b55SLin Ling (void) snprintf(propbuf, proplen, "%llu", propvalue); 3176edea4b55SLin Ling } else if (propvalue == 0 && 3177f67950b2SNasf-Fan (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA || 3178f67950b2SNasf-Fan type == ZFS_PROP_USEROBJQUOTA || type == ZFS_PROP_GROUPOBJQUOTA || 3179f67950b2SNasf-Fan type == ZFS_PROP_PROJECTQUOTA || ZFS_PROP_PROJECTOBJQUOTA)) { 318014843421SMatthew Ahrens (void) strlcpy(propbuf, "none", proplen); 3181f67950b2SNasf-Fan } else if (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA || 3182f67950b2SNasf-Fan type == ZFS_PROP_USERUSED || type == ZFS_PROP_GROUPUSED || 3183f67950b2SNasf-Fan type == ZFS_PROP_PROJECTUSED || type == ZFS_PROP_PROJECTQUOTA) { 3184f67950b2SNasf-Fan zfs_nicenum(propvalue, propbuf, proplen); 318514843421SMatthew Ahrens } else { 3186edea4b55SLin Ling zfs_nicenum(propvalue, propbuf, proplen); 318714843421SMatthew Ahrens } 318814843421SMatthew Ahrens return (0); 318914843421SMatthew Ahrens } 319014843421SMatthew Ahrens 319119b94df9SMatthew Ahrens int 319219b94df9SMatthew Ahrens zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname, 319319b94df9SMatthew Ahrens uint64_t *propvalue) 319419b94df9SMatthew Ahrens { 319519b94df9SMatthew Ahrens int err; 319619b94df9SMatthew Ahrens zfs_cmd_t zc = { 0 }; 319719b94df9SMatthew Ahrens const char *snapname; 319819b94df9SMatthew Ahrens 319919b94df9SMatthew Ahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 320019b94df9SMatthew Ahrens 320119b94df9SMatthew Ahrens snapname = strchr(propname, '@') + 1; 320219b94df9SMatthew Ahrens if (strchr(snapname, '@')) { 320319b94df9SMatthew Ahrens (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value)); 320419b94df9SMatthew Ahrens } else { 320519b94df9SMatthew Ahrens /* snapname is the short name, append it to zhp's fsname */ 320619b94df9SMatthew Ahrens char *cp; 320719b94df9SMatthew Ahrens 320819b94df9SMatthew Ahrens (void) strlcpy(zc.zc_value, zhp->zfs_name, 320919b94df9SMatthew Ahrens sizeof (zc.zc_value)); 321019b94df9SMatthew Ahrens cp = strchr(zc.zc_value, '@'); 321119b94df9SMatthew Ahrens if (cp != NULL) 321219b94df9SMatthew Ahrens *cp = '\0'; 321319b94df9SMatthew Ahrens (void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value)); 321419b94df9SMatthew Ahrens (void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value)); 321519b94df9SMatthew Ahrens } 321619b94df9SMatthew Ahrens 321719b94df9SMatthew Ahrens err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc); 321819b94df9SMatthew Ahrens if (err) 321919b94df9SMatthew Ahrens return (err); 322019b94df9SMatthew Ahrens 322119b94df9SMatthew Ahrens *propvalue = zc.zc_cookie; 322219b94df9SMatthew Ahrens return (0); 322319b94df9SMatthew Ahrens } 322419b94df9SMatthew Ahrens 322519b94df9SMatthew Ahrens int 322619b94df9SMatthew Ahrens zfs_prop_get_written(zfs_handle_t *zhp, const char *propname, 322719b94df9SMatthew Ahrens char *propbuf, int proplen, boolean_t literal) 322819b94df9SMatthew Ahrens { 322919b94df9SMatthew Ahrens int err; 323019b94df9SMatthew Ahrens uint64_t propvalue; 323119b94df9SMatthew Ahrens 323219b94df9SMatthew Ahrens err = zfs_prop_get_written_int(zhp, propname, &propvalue); 323319b94df9SMatthew Ahrens 323419b94df9SMatthew Ahrens if (err) 323519b94df9SMatthew Ahrens return (err); 323619b94df9SMatthew Ahrens 323719b94df9SMatthew Ahrens if (literal) { 323819b94df9SMatthew Ahrens (void) snprintf(propbuf, proplen, "%llu", propvalue); 323919b94df9SMatthew Ahrens } else { 324019b94df9SMatthew Ahrens zfs_nicenum(propvalue, propbuf, proplen); 324119b94df9SMatthew Ahrens } 324219b94df9SMatthew Ahrens return (0); 324319b94df9SMatthew Ahrens } 324419b94df9SMatthew Ahrens 3245fa9e4066Sahrens /* 3246fa9e4066Sahrens * Returns the name of the given zfs handle. 3247fa9e4066Sahrens */ 3248fa9e4066Sahrens const char * 3249fa9e4066Sahrens zfs_get_name(const zfs_handle_t *zhp) 3250fa9e4066Sahrens { 3251fa9e4066Sahrens return (zhp->zfs_name); 3252fa9e4066Sahrens } 3253fa9e4066Sahrens 3254fa9e4066Sahrens /* 32558808ac5dSYuri Pankov * Returns the name of the parent pool for the given zfs handle. 32568808ac5dSYuri Pankov */ 32578808ac5dSYuri Pankov const char * 32588808ac5dSYuri Pankov zfs_get_pool_name(const zfs_handle_t *zhp) 32598808ac5dSYuri Pankov { 32608808ac5dSYuri Pankov return (zhp->zpool_hdl->zpool_name); 32618808ac5dSYuri Pankov } 32628808ac5dSYuri Pankov 32638808ac5dSYuri Pankov /* 3264fa9e4066Sahrens * Returns the type of the given zfs handle. 3265fa9e4066Sahrens */ 3266fa9e4066Sahrens zfs_type_t 3267fa9e4066Sahrens zfs_get_type(const zfs_handle_t *zhp) 3268fa9e4066Sahrens { 3269fa9e4066Sahrens return (zhp->zfs_type); 3270fa9e4066Sahrens } 3271fa9e4066Sahrens 32727f7322feSeschrock /* 3273d41c4376SMark J Musante * Is one dataset name a child dataset of another? 3274d41c4376SMark J Musante * 3275d41c4376SMark J Musante * Needs to handle these cases: 3276d41c4376SMark J Musante * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo" 3277d41c4376SMark J Musante * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar" 3278d41c4376SMark J Musante * Descendant? No. No. No. Yes. 3279d41c4376SMark J Musante */ 3280d41c4376SMark J Musante static boolean_t 3281d41c4376SMark J Musante is_descendant(const char *ds1, const char *ds2) 3282d41c4376SMark J Musante { 3283d41c4376SMark J Musante size_t d1len = strlen(ds1); 3284d41c4376SMark J Musante 3285d41c4376SMark J Musante /* ds2 can't be a descendant if it's smaller */ 3286d41c4376SMark J Musante if (strlen(ds2) < d1len) 3287d41c4376SMark J Musante return (B_FALSE); 3288d41c4376SMark J Musante 3289d41c4376SMark J Musante /* otherwise, compare strings and verify that there's a '/' char */ 3290d41c4376SMark J Musante return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0)); 3291d41c4376SMark J Musante } 3292d41c4376SMark J Musante 3293d41c4376SMark J Musante /* 3294fa9e4066Sahrens * Given a complete name, return just the portion that refers to the parent. 329519b94df9SMatthew Ahrens * Will return -1 if there is no parent (path is just the name of the 329619b94df9SMatthew Ahrens * pool). 3297fa9e4066Sahrens */ 3298fa9e4066Sahrens static int 3299fa9e4066Sahrens parent_name(const char *path, char *buf, size_t buflen) 3300fa9e4066Sahrens { 330119b94df9SMatthew Ahrens char *slashp; 3302fa9e4066Sahrens 330319b94df9SMatthew Ahrens (void) strlcpy(buf, path, buflen); 330419b94df9SMatthew Ahrens 330519b94df9SMatthew Ahrens if ((slashp = strrchr(buf, '/')) == NULL) 3306fa9e4066Sahrens return (-1); 330719b94df9SMatthew Ahrens *slashp = '\0'; 3308fa9e4066Sahrens 3309fa9e4066Sahrens return (0); 3310fa9e4066Sahrens } 3311fa9e4066Sahrens 3312eb633035STom Caputi int 3313eb633035STom Caputi zfs_parent_name(zfs_handle_t *zhp, char *buf, size_t buflen) 3314eb633035STom Caputi { 3315eb633035STom Caputi return (parent_name(zfs_get_name(zhp), buf, buflen)); 3316eb633035STom Caputi } 3317eb633035STom Caputi 3318fa9e4066Sahrens /* 33197f1f55eaSvb160487 * If accept_ancestor is false, then check to make sure that the given path has 33207f1f55eaSvb160487 * a parent, and that it exists. If accept_ancestor is true, then find the 33217f1f55eaSvb160487 * closest existing ancestor for the given path. In prefixlen return the 33227f1f55eaSvb160487 * length of already existing prefix of the given path. We also fetch the 33237f1f55eaSvb160487 * 'zoned' property, which is used to validate property settings when creating 33247f1f55eaSvb160487 * new datasets. 3325fa9e4066Sahrens */ 3326fa9e4066Sahrens static int 33277f1f55eaSvb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned, 33287f1f55eaSvb160487 boolean_t accept_ancestor, int *prefixlen) 3329fa9e4066Sahrens { 3330fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 33319adfa60dSMatthew Ahrens char parent[ZFS_MAX_DATASET_NAME_LEN]; 3332fa9e4066Sahrens char *slash; 33337f7322feSeschrock zfs_handle_t *zhp; 333499653d4eSeschrock char errbuf[1024]; 3335d41c4376SMark J Musante uint64_t is_zoned; 333699653d4eSeschrock 3337deb8317bSMark J Musante (void) snprintf(errbuf, sizeof (errbuf), 3338deb8317bSMark J Musante dgettext(TEXT_DOMAIN, "cannot create '%s'"), path); 3339fa9e4066Sahrens 3340fa9e4066Sahrens /* get parent, and check to see if this is just a pool */ 3341fa9e4066Sahrens if (parent_name(path, parent, sizeof (parent)) != 0) { 334299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 334399653d4eSeschrock "missing dataset name")); 334499653d4eSeschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3345fa9e4066Sahrens } 3346fa9e4066Sahrens 3347fa9e4066Sahrens /* check to see if the pool exists */ 3348fa9e4066Sahrens if ((slash = strchr(parent, '/')) == NULL) 3349fa9e4066Sahrens slash = parent + strlen(parent); 33508ac09fceSRichard Lowe (void) strncpy(zc.zc_name, parent, slash - parent); 3351fa9e4066Sahrens zc.zc_name[slash - parent] = '\0'; 335299653d4eSeschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 && 3353fa9e4066Sahrens errno == ENOENT) { 335499653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 335599653d4eSeschrock "no such pool '%s'"), zc.zc_name); 335699653d4eSeschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 3357fa9e4066Sahrens } 3358fa9e4066Sahrens 3359fa9e4066Sahrens /* check to see if the parent dataset exists */ 33607f1f55eaSvb160487 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) { 33617f1f55eaSvb160487 if (errno == ENOENT && accept_ancestor) { 33627f1f55eaSvb160487 /* 33637f1f55eaSvb160487 * Go deeper to find an ancestor, give up on top level. 33647f1f55eaSvb160487 */ 33657f1f55eaSvb160487 if (parent_name(parent, parent, sizeof (parent)) != 0) { 33667f1f55eaSvb160487 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 33677f1f55eaSvb160487 "no such pool '%s'"), zc.zc_name); 33687f1f55eaSvb160487 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 33697f1f55eaSvb160487 } 33707f1f55eaSvb160487 } else if (errno == ENOENT) { 337199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 337299653d4eSeschrock "parent does not exist")); 337399653d4eSeschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 33747f1f55eaSvb160487 } else 337599653d4eSeschrock return (zfs_standard_error(hdl, errno, errbuf)); 3376fa9e4066Sahrens } 3377fa9e4066Sahrens 3378d41c4376SMark J Musante is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); 3379d41c4376SMark J Musante if (zoned != NULL) 3380d41c4376SMark J Musante *zoned = is_zoned; 3381d41c4376SMark J Musante 3382fa9e4066Sahrens /* we are in a non-global zone, but parent is in the global zone */ 3383d41c4376SMark J Musante if (getzoneid() != GLOBAL_ZONEID && !is_zoned) { 338499653d4eSeschrock (void) zfs_standard_error(hdl, EPERM, errbuf); 33857f7322feSeschrock zfs_close(zhp); 3386fa9e4066Sahrens return (-1); 3387fa9e4066Sahrens } 3388fa9e4066Sahrens 3389fa9e4066Sahrens /* make sure parent is a filesystem */ 33907f7322feSeschrock if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) { 339199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 339299653d4eSeschrock "parent is not a filesystem")); 339399653d4eSeschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 33947f7322feSeschrock zfs_close(zhp); 3395fa9e4066Sahrens return (-1); 3396fa9e4066Sahrens } 3397fa9e4066Sahrens 33987f7322feSeschrock zfs_close(zhp); 33997f1f55eaSvb160487 if (prefixlen != NULL) 34007f1f55eaSvb160487 *prefixlen = strlen(parent); 34017f1f55eaSvb160487 return (0); 34027f1f55eaSvb160487 } 34037f1f55eaSvb160487 34047f1f55eaSvb160487 /* 34057f1f55eaSvb160487 * Finds whether the dataset of the given type(s) exists. 34067f1f55eaSvb160487 */ 34077f1f55eaSvb160487 boolean_t 34087f1f55eaSvb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types) 34097f1f55eaSvb160487 { 34107f1f55eaSvb160487 zfs_handle_t *zhp; 34117f1f55eaSvb160487 3412f18faf3fSek110237 if (!zfs_validate_name(hdl, path, types, B_FALSE)) 34137f1f55eaSvb160487 return (B_FALSE); 34147f1f55eaSvb160487 34157f1f55eaSvb160487 /* 34167f1f55eaSvb160487 * Try to get stats for the dataset, which will tell us if it exists. 34177f1f55eaSvb160487 */ 34187f1f55eaSvb160487 if ((zhp = make_dataset_handle(hdl, path)) != NULL) { 34197f1f55eaSvb160487 int ds_type = zhp->zfs_type; 34207f1f55eaSvb160487 34217f1f55eaSvb160487 zfs_close(zhp); 34227f1f55eaSvb160487 if (types & ds_type) 34237f1f55eaSvb160487 return (B_TRUE); 34247f1f55eaSvb160487 } 34257f1f55eaSvb160487 return (B_FALSE); 34267f1f55eaSvb160487 } 34277f1f55eaSvb160487 34287f1f55eaSvb160487 /* 34293cb34c60Sahrens * Given a path to 'target', create all the ancestors between 34303cb34c60Sahrens * the prefixlen portion of the path, and the target itself. 34313cb34c60Sahrens * Fail if the initial prefixlen-ancestor does not already exist. 34323cb34c60Sahrens */ 34333cb34c60Sahrens int 34343cb34c60Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen) 34353cb34c60Sahrens { 34363cb34c60Sahrens zfs_handle_t *h; 34373cb34c60Sahrens char *cp; 34383cb34c60Sahrens const char *opname; 34393cb34c60Sahrens 34403cb34c60Sahrens /* make sure prefix exists */ 34413cb34c60Sahrens cp = target + prefixlen; 34423cb34c60Sahrens if (*cp != '/') { 34433cb34c60Sahrens assert(strchr(cp, '/') == NULL); 34443cb34c60Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 34453cb34c60Sahrens } else { 34463cb34c60Sahrens *cp = '\0'; 34473cb34c60Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 34483cb34c60Sahrens *cp = '/'; 34493cb34c60Sahrens } 34503cb34c60Sahrens if (h == NULL) 34513cb34c60Sahrens return (-1); 34523cb34c60Sahrens zfs_close(h); 34533cb34c60Sahrens 34543cb34c60Sahrens /* 34553cb34c60Sahrens * Attempt to create, mount, and share any ancestor filesystems, 34563cb34c60Sahrens * up to the prefixlen-long one. 34573cb34c60Sahrens */ 34583cb34c60Sahrens for (cp = target + prefixlen + 1; 345988f61deeSIgor Kozhukhov (cp = strchr(cp, '/')) != NULL; *cp = '/', cp++) { 34603cb34c60Sahrens 34613cb34c60Sahrens *cp = '\0'; 34623cb34c60Sahrens 34633cb34c60Sahrens h = make_dataset_handle(hdl, target); 34643cb34c60Sahrens if (h) { 34653cb34c60Sahrens /* it already exists, nothing to do here */ 34663cb34c60Sahrens zfs_close(h); 34673cb34c60Sahrens continue; 34683cb34c60Sahrens } 34693cb34c60Sahrens 34703cb34c60Sahrens if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM, 34713cb34c60Sahrens NULL) != 0) { 34723cb34c60Sahrens opname = dgettext(TEXT_DOMAIN, "create"); 34733cb34c60Sahrens goto ancestorerr; 34743cb34c60Sahrens } 34753cb34c60Sahrens 34763cb34c60Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 34773cb34c60Sahrens if (h == NULL) { 34783cb34c60Sahrens opname = dgettext(TEXT_DOMAIN, "open"); 34793cb34c60Sahrens goto ancestorerr; 34803cb34c60Sahrens } 34813cb34c60Sahrens 34823cb34c60Sahrens if (zfs_mount(h, NULL, 0) != 0) { 34833cb34c60Sahrens opname = dgettext(TEXT_DOMAIN, "mount"); 34843cb34c60Sahrens goto ancestorerr; 34853cb34c60Sahrens } 34863cb34c60Sahrens 34873cb34c60Sahrens if (zfs_share(h) != 0) { 34883cb34c60Sahrens opname = dgettext(TEXT_DOMAIN, "share"); 34893cb34c60Sahrens goto ancestorerr; 34903cb34c60Sahrens } 34913cb34c60Sahrens 34923cb34c60Sahrens zfs_close(h); 34933cb34c60Sahrens } 34943cb34c60Sahrens 34953cb34c60Sahrens return (0); 34963cb34c60Sahrens 34973cb34c60Sahrens ancestorerr: 34983cb34c60Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 34993cb34c60Sahrens "failed to %s ancestor '%s'"), opname, target); 35003cb34c60Sahrens return (-1); 35013cb34c60Sahrens } 35023cb34c60Sahrens 35033cb34c60Sahrens /* 35047f1f55eaSvb160487 * Creates non-existing ancestors of the given path. 35057f1f55eaSvb160487 */ 35067f1f55eaSvb160487 int 35077f1f55eaSvb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path) 35087f1f55eaSvb160487 { 35097f1f55eaSvb160487 int prefix; 35107f1f55eaSvb160487 char *path_copy; 35115ac95da7SSerapheim Dimitropoulos char errbuf[1024]; 3512f83b46baSPaul Dagnelie int rc = 0; 35137f1f55eaSvb160487 35145ac95da7SSerapheim Dimitropoulos (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 35155ac95da7SSerapheim Dimitropoulos "cannot create '%s'"), path); 35165ac95da7SSerapheim Dimitropoulos 35175ac95da7SSerapheim Dimitropoulos /* 35185ac95da7SSerapheim Dimitropoulos * Check that we are not passing the nesting limit 35195ac95da7SSerapheim Dimitropoulos * before we start creating any ancestors. 35205ac95da7SSerapheim Dimitropoulos */ 35215ac95da7SSerapheim Dimitropoulos if (dataset_nestcheck(path) != 0) { 35225ac95da7SSerapheim Dimitropoulos zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35235ac95da7SSerapheim Dimitropoulos "maximum name nesting depth exceeded")); 35245ac95da7SSerapheim Dimitropoulos return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 35255ac95da7SSerapheim Dimitropoulos } 35265ac95da7SSerapheim Dimitropoulos 3527d41c4376SMark J Musante if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0) 35287f1f55eaSvb160487 return (-1); 35297f1f55eaSvb160487 35307f1f55eaSvb160487 if ((path_copy = strdup(path)) != NULL) { 35317f1f55eaSvb160487 rc = create_parents(hdl, path_copy, prefix); 35327f1f55eaSvb160487 free(path_copy); 35337f1f55eaSvb160487 } 35347f1f55eaSvb160487 if (path_copy == NULL || rc != 0) 35357f1f55eaSvb160487 return (-1); 35367f1f55eaSvb160487 3537fa9e4066Sahrens return (0); 3538fa9e4066Sahrens } 3539fa9e4066Sahrens 3540fa9e4066Sahrens /* 3541e9dbad6fSeschrock * Create a new filesystem or volume. 3542fa9e4066Sahrens */ 3543fa9e4066Sahrens int 354499653d4eSeschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type, 3545e9dbad6fSeschrock nvlist_t *props) 3546fa9e4066Sahrens { 3547fa9e4066Sahrens int ret; 3548fa9e4066Sahrens uint64_t size = 0; 3549fa9e4066Sahrens uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 3550eb633035STom Caputi uint8_t *wkeydata = NULL; 3551eb633035STom Caputi uint_t wkeylen = 0; 355299653d4eSeschrock char errbuf[1024]; 3553eb633035STom Caputi char parent[MAXNAMELEN]; 3554e9dbad6fSeschrock uint64_t zoned; 355526455f9eSAndriy Gapon enum lzc_dataset_type ost; 3556690031d3Sloli10K zpool_handle_t *zpool_handle; 355799653d4eSeschrock 355899653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 355999653d4eSeschrock "cannot create '%s'"), path); 3560fa9e4066Sahrens 3561fa9e4066Sahrens /* validate the path, taking care to note the extended error message */ 3562f18faf3fSek110237 if (!zfs_validate_name(hdl, path, type, B_TRUE)) 356399653d4eSeschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3564fa9e4066Sahrens 35655ac95da7SSerapheim Dimitropoulos if (dataset_nestcheck(path) != 0) { 35665ac95da7SSerapheim Dimitropoulos zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35675ac95da7SSerapheim Dimitropoulos "maximum name nesting depth exceeded")); 35685ac95da7SSerapheim Dimitropoulos return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 35695ac95da7SSerapheim Dimitropoulos } 35705ac95da7SSerapheim Dimitropoulos 3571fa9e4066Sahrens /* validate parents exist */ 35727f1f55eaSvb160487 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0) 3573fa9e4066Sahrens return (-1); 3574fa9e4066Sahrens 3575fa9e4066Sahrens /* 3576fa9e4066Sahrens * The failure modes when creating a dataset of a different type over 3577fa9e4066Sahrens * one that already exists is a little strange. In particular, if you 3578fa9e4066Sahrens * try to create a dataset on top of an existing dataset, the ioctl() 3579fa9e4066Sahrens * will return ENOENT, not EEXIST. To prevent this from happening, we 3580fa9e4066Sahrens * first try to see if the dataset exists. 3581fa9e4066Sahrens */ 35824445fffbSMatthew Ahrens if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) { 358399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 358499653d4eSeschrock "dataset already exists")); 358599653d4eSeschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3586fa9e4066Sahrens } 3587fa9e4066Sahrens 3588fa9e4066Sahrens if (type == ZFS_TYPE_VOLUME) 358926455f9eSAndriy Gapon ost = LZC_DATSET_TYPE_ZVOL; 3590fa9e4066Sahrens else 359126455f9eSAndriy Gapon ost = LZC_DATSET_TYPE_ZFS; 3592fa9e4066Sahrens 3593e9316f76SJoe Stein /* open zpool handle for prop validation */ 35949adfa60dSMatthew Ahrens char pool_path[ZFS_MAX_DATASET_NAME_LEN]; 3595e9316f76SJoe Stein (void) strlcpy(pool_path, path, sizeof (pool_path)); 3596e9316f76SJoe Stein 3597e9316f76SJoe Stein /* truncate pool_path at first slash */ 3598e9316f76SJoe Stein char *p = strchr(pool_path, '/'); 3599e9316f76SJoe Stein if (p != NULL) 3600e9316f76SJoe Stein *p = '\0'; 3601e9316f76SJoe Stein 3602690031d3Sloli10K if ((zpool_handle = zpool_open(hdl, pool_path)) == NULL) 3603690031d3Sloli10K return (-1); 3604e9316f76SJoe Stein 36050a48a24eStimh if (props && (props = zfs_valid_proplist(hdl, type, props, 3606eb633035STom Caputi zoned, NULL, zpool_handle, B_TRUE, errbuf)) == 0) { 3607e9316f76SJoe Stein zpool_close(zpool_handle); 3608e9dbad6fSeschrock return (-1); 3609e9316f76SJoe Stein } 3610e9316f76SJoe Stein zpool_close(zpool_handle); 3611e9dbad6fSeschrock 3612fa9e4066Sahrens if (type == ZFS_TYPE_VOLUME) { 36135c5460e9Seschrock /* 36145c5460e9Seschrock * If we are creating a volume, the size and block size must 36155c5460e9Seschrock * satisfy a few restraints. First, the blocksize must be a 36165c5460e9Seschrock * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the 36175c5460e9Seschrock * volsize must be a multiple of the block size, and cannot be 36185c5460e9Seschrock * zero. 36195c5460e9Seschrock */ 3620e9dbad6fSeschrock if (props == NULL || nvlist_lookup_uint64(props, 3621e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) { 3622e9dbad6fSeschrock nvlist_free(props); 362399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3624e9dbad6fSeschrock "missing volume size")); 3625e9dbad6fSeschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 3626fa9e4066Sahrens } 3627fa9e4066Sahrens 3628e9dbad6fSeschrock if ((ret = nvlist_lookup_uint64(props, 3629e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 3630e9dbad6fSeschrock &blocksize)) != 0) { 3631e9dbad6fSeschrock if (ret == ENOENT) { 3632e9dbad6fSeschrock blocksize = zfs_prop_default_numeric( 3633e9dbad6fSeschrock ZFS_PROP_VOLBLOCKSIZE); 3634e9dbad6fSeschrock } else { 3635e9dbad6fSeschrock nvlist_free(props); 363699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3637e9dbad6fSeschrock "missing volume block size")); 3638e9dbad6fSeschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 3639e9dbad6fSeschrock } 3640e9dbad6fSeschrock } 3641e9dbad6fSeschrock 3642e9dbad6fSeschrock if (size == 0) { 3643e9dbad6fSeschrock nvlist_free(props); 3644e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3645e9dbad6fSeschrock "volume size cannot be zero")); 3646e9dbad6fSeschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 36475c5460e9Seschrock } 36485c5460e9Seschrock 36495c5460e9Seschrock if (size % blocksize != 0) { 3650e9dbad6fSeschrock nvlist_free(props); 365199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3652e9dbad6fSeschrock "volume size must be a multiple of volume block " 3653e9dbad6fSeschrock "size")); 3654e9dbad6fSeschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 3655e9dbad6fSeschrock } 36565c5460e9Seschrock } 36575c5460e9Seschrock 3658eb633035STom Caputi (void) parent_name(path, parent, sizeof (parent)); 36596ccda740Sloli10K if (zfs_crypto_create(hdl, parent, props, NULL, B_TRUE, 36606ccda740Sloli10K &wkeydata, &wkeylen) != 0) { 36614445fffbSMatthew Ahrens nvlist_free(props); 3662eb633035STom Caputi return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf)); 3663eb633035STom Caputi } 3664eb633035STom Caputi 3665eb633035STom Caputi /* create the dataset */ 3666eb633035STom Caputi ret = lzc_create(path, ost, props, wkeydata, wkeylen); 3667eb633035STom Caputi nvlist_free(props); 3668eb633035STom Caputi if (wkeydata != NULL) 3669eb633035STom Caputi free(wkeydata); 3670e9dbad6fSeschrock 3671fa9e4066Sahrens /* check for failure */ 3672fa9e4066Sahrens if (ret != 0) { 3673fa9e4066Sahrens switch (errno) { 3674fa9e4066Sahrens case ENOENT: 367599653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 367699653d4eSeschrock "no such parent '%s'"), parent); 367799653d4eSeschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 3678fa9e4066Sahrens 3679fa9e4066Sahrens case EINVAL: 368099653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3681d7d4af51Smmusante "parent '%s' is not a filesystem"), parent); 368299653d4eSeschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 3683fa9e4066Sahrens 368440feaa91Sahrens case ENOTSUP: 368540feaa91Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 368640feaa91Sahrens "pool must be upgraded to set this " 368740feaa91Sahrens "property or value")); 368840feaa91Sahrens return (zfs_error(hdl, EZFS_BADVERSION, errbuf)); 36899fa2266dSYuri Pankov case ERANGE: 36909fa2266dSYuri Pankov zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 36919fa2266dSYuri Pankov "invalid property value(s) specified")); 36929fa2266dSYuri Pankov return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 3693eb633035STom Caputi case EACCES: 3694eb633035STom Caputi zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3695eb633035STom Caputi "encryption root's key is not loaded " 3696eb633035STom Caputi "or provided")); 3697eb633035STom Caputi return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf)); 3698eb633035STom Caputi 3699fa9e4066Sahrens #ifdef _ILP32 3700fa9e4066Sahrens case EOVERFLOW: 3701fa9e4066Sahrens /* 3702fa9e4066Sahrens * This platform can't address a volume this big. 3703fa9e4066Sahrens */ 370499653d4eSeschrock if (type == ZFS_TYPE_VOLUME) 370599653d4eSeschrock return (zfs_error(hdl, EZFS_VOLTOOBIG, 370699653d4eSeschrock errbuf)); 3707fa9e4066Sahrens #endif 370899653d4eSeschrock /* FALLTHROUGH */ 3709fa9e4066Sahrens default: 371099653d4eSeschrock return (zfs_standard_error(hdl, errno, errbuf)); 3711fa9e4066Sahrens } 3712fa9e4066Sahrens } 3713fa9e4066Sahrens 3714fa9e4066Sahrens return (0); 3715fa9e4066Sahrens } 3716fa9e4066Sahrens 3717fa9e4066Sahrens /* 3718fa9e4066Sahrens * Destroys the given dataset. The caller must make sure that the filesystem 371965fec9f6SChristopher Siden * isn't mounted, and that there are no active dependents. If the file system 372065fec9f6SChristopher Siden * does not exist this function does nothing. 3721fa9e4066Sahrens */ 3722fa9e4066Sahrens int 3723842727c2SChris Kirby zfs_destroy(zfs_handle_t *zhp, boolean_t defer) 3724fa9e4066Sahrens { 3725049ba636SAndriy Gapon int error; 3726049ba636SAndriy Gapon 3727049ba636SAndriy Gapon if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT && defer) 3728049ba636SAndriy Gapon return (EINVAL); 3729fa9e4066Sahrens 373078f17100SMatthew Ahrens if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) { 373178f17100SMatthew Ahrens nvlist_t *nv = fnvlist_alloc(); 373278f17100SMatthew Ahrens fnvlist_add_boolean(nv, zhp->zfs_name); 3733049ba636SAndriy Gapon error = lzc_destroy_bookmarks(nv, NULL); 373478f17100SMatthew Ahrens fnvlist_free(nv); 373578f17100SMatthew Ahrens if (error != 0) { 3736049ba636SAndriy Gapon return (zfs_standard_error_fmt(zhp->zfs_hdl, error, 373778f17100SMatthew Ahrens dgettext(TEXT_DOMAIN, "cannot destroy '%s'"), 373878f17100SMatthew Ahrens zhp->zfs_name)); 373978f17100SMatthew Ahrens } 374078f17100SMatthew Ahrens return (0); 374178f17100SMatthew Ahrens } 374278f17100SMatthew Ahrens 3743049ba636SAndriy Gapon if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 3744049ba636SAndriy Gapon nvlist_t *nv = fnvlist_alloc(); 3745049ba636SAndriy Gapon fnvlist_add_boolean(nv, zhp->zfs_name); 3746049ba636SAndriy Gapon error = lzc_destroy_snaps(nv, defer, NULL); 3747049ba636SAndriy Gapon fnvlist_free(nv); 3748fa9e4066Sahrens } else { 3749049ba636SAndriy Gapon error = lzc_destroy(zhp->zfs_name); 3750fa9e4066Sahrens } 3751fa9e4066Sahrens 3752049ba636SAndriy Gapon if (error != 0 && error != ENOENT) { 3753ece3d9b3Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, errno, 375499653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot destroy '%s'"), 375599653d4eSeschrock zhp->zfs_name)); 37561d452cf5Sahrens } 3757fa9e4066Sahrens 3758fa9e4066Sahrens remove_mountpoint(zhp); 3759fa9e4066Sahrens 3760fa9e4066Sahrens return (0); 3761fa9e4066Sahrens } 3762fa9e4066Sahrens 37631d452cf5Sahrens struct destroydata { 376419b94df9SMatthew Ahrens nvlist_t *nvl; 376519b94df9SMatthew Ahrens const char *snapname; 37661d452cf5Sahrens }; 37671d452cf5Sahrens 37681d452cf5Sahrens static int 3769681d9761SEric Taylor zfs_check_snap_cb(zfs_handle_t *zhp, void *arg) 37701d452cf5Sahrens { 37711d452cf5Sahrens struct destroydata *dd = arg; 37729adfa60dSMatthew Ahrens char name[ZFS_MAX_DATASET_NAME_LEN]; 3773681d9761SEric Taylor int rv = 0; 37741d452cf5Sahrens 377519b94df9SMatthew Ahrens (void) snprintf(name, sizeof (name), 377619b94df9SMatthew Ahrens "%s@%s", zhp->zfs_name, dd->snapname); 37771d452cf5Sahrens 3778a7a845e4SSteven Hartland if (lzc_exists(name)) 377919b94df9SMatthew Ahrens verify(nvlist_add_boolean(dd->nvl, name) == 0); 37801d452cf5Sahrens 378119b94df9SMatthew Ahrens rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd); 37823ccfa83cSahrens zfs_close(zhp); 37833ccfa83cSahrens return (rv); 37841d452cf5Sahrens } 37851d452cf5Sahrens 37861d452cf5Sahrens /* 37871d452cf5Sahrens * Destroys all snapshots with the given name in zhp & descendants. 37881d452cf5Sahrens */ 37891d452cf5Sahrens int 3790842727c2SChris Kirby zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer) 37911d452cf5Sahrens { 37921d452cf5Sahrens int ret; 37931d452cf5Sahrens struct destroydata dd = { 0 }; 37941d452cf5Sahrens 37951d452cf5Sahrens dd.snapname = snapname; 379619b94df9SMatthew Ahrens verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0); 379719b94df9SMatthew Ahrens (void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd); 37981d452cf5Sahrens 3799a7a845e4SSteven Hartland if (nvlist_empty(dd.nvl)) { 380019b94df9SMatthew Ahrens ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT, 38011d452cf5Sahrens dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"), 380219b94df9SMatthew Ahrens zhp->zfs_name, snapname); 380319b94df9SMatthew Ahrens } else { 38043b2aab18SMatthew Ahrens ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer); 380519b94df9SMatthew Ahrens } 380619b94df9SMatthew Ahrens nvlist_free(dd.nvl); 380719b94df9SMatthew Ahrens return (ret); 3808e5351341SMatthew Ahrens } 3809e5351341SMatthew Ahrens 381019b94df9SMatthew Ahrens /* 38113b2aab18SMatthew Ahrens * Destroys all the snapshots named in the nvlist. 381219b94df9SMatthew Ahrens */ 381319b94df9SMatthew Ahrens int 38143b2aab18SMatthew Ahrens zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer) 381519b94df9SMatthew Ahrens { 381619b94df9SMatthew Ahrens int ret; 38174cde22c2SChris Williamson nvlist_t *errlist = NULL; 381819b94df9SMatthew Ahrens 38194445fffbSMatthew Ahrens ret = lzc_destroy_snaps(snaps, defer, &errlist); 38201d452cf5Sahrens 38214cde22c2SChris Williamson if (ret == 0) { 38224cde22c2SChris Williamson nvlist_free(errlist); 38233b2aab18SMatthew Ahrens return (0); 38244cde22c2SChris Williamson } 38253b2aab18SMatthew Ahrens 3826a7a845e4SSteven Hartland if (nvlist_empty(errlist)) { 38273b2aab18SMatthew Ahrens char errbuf[1024]; 38283b2aab18SMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), 38293b2aab18SMatthew Ahrens dgettext(TEXT_DOMAIN, "cannot destroy snapshots")); 38303b2aab18SMatthew Ahrens 38313b2aab18SMatthew Ahrens ret = zfs_standard_error(hdl, ret, errbuf); 38323b2aab18SMatthew Ahrens } 38334445fffbSMatthew Ahrens for (nvpair_t *pair = nvlist_next_nvpair(errlist, NULL); 38344445fffbSMatthew Ahrens pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) { 38351d452cf5Sahrens char errbuf[1024]; 38364445fffbSMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), 38374445fffbSMatthew Ahrens dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"), 38384445fffbSMatthew Ahrens nvpair_name(pair)); 38391d452cf5Sahrens 38404445fffbSMatthew Ahrens switch (fnvpair_value_int32(pair)) { 38411d452cf5Sahrens case EEXIST: 38423b2aab18SMatthew Ahrens zfs_error_aux(hdl, 38433b2aab18SMatthew Ahrens dgettext(TEXT_DOMAIN, "snapshot is cloned")); 38443b2aab18SMatthew Ahrens ret = zfs_error(hdl, EZFS_EXISTS, errbuf); 38454445fffbSMatthew Ahrens break; 38461d452cf5Sahrens default: 38473b2aab18SMatthew Ahrens ret = zfs_standard_error(hdl, errno, errbuf); 38484445fffbSMatthew Ahrens break; 38494445fffbSMatthew Ahrens } 38501d452cf5Sahrens } 38511d452cf5Sahrens 38524cde22c2SChris Williamson nvlist_free(errlist); 38534445fffbSMatthew Ahrens return (ret); 38541d452cf5Sahrens } 38551d452cf5Sahrens 3856fa9e4066Sahrens /* 3857fa9e4066Sahrens * Clones the given dataset. The target must be of the same type as the source. 3858fa9e4066Sahrens */ 3859fa9e4066Sahrens int 3860e9dbad6fSeschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props) 3861fa9e4066Sahrens { 38629adfa60dSMatthew Ahrens char parent[ZFS_MAX_DATASET_NAME_LEN]; 3863fa9e4066Sahrens int ret; 386499653d4eSeschrock char errbuf[1024]; 386599653d4eSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 3866e9dbad6fSeschrock uint64_t zoned; 3867fa9e4066Sahrens 3868fa9e4066Sahrens assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 3869fa9e4066Sahrens 387099653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 387199653d4eSeschrock "cannot create '%s'"), target); 387299653d4eSeschrock 387319b94df9SMatthew Ahrens /* validate the target/clone name */ 3874f18faf3fSek110237 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE)) 387599653d4eSeschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3876fa9e4066Sahrens 3877fa9e4066Sahrens /* validate parents exist */ 38787f1f55eaSvb160487 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0) 3879fa9e4066Sahrens return (-1); 3880fa9e4066Sahrens 3881fa9e4066Sahrens (void) parent_name(target, parent, sizeof (parent)); 3882fa9e4066Sahrens 3883fa9e4066Sahrens /* do the clone */ 3884e9dbad6fSeschrock 3885e9dbad6fSeschrock if (props) { 38864445fffbSMatthew Ahrens zfs_type_t type; 38871c10ae76SMike Gerdts 38884445fffbSMatthew Ahrens if (ZFS_IS_VOLUME(zhp)) { 38894445fffbSMatthew Ahrens type = ZFS_TYPE_VOLUME; 38904445fffbSMatthew Ahrens } else { 38914445fffbSMatthew Ahrens type = ZFS_TYPE_FILESYSTEM; 38924445fffbSMatthew Ahrens } 38930a48a24eStimh if ((props = zfs_valid_proplist(hdl, type, props, zoned, 3894eb633035STom Caputi zhp, zhp->zpool_hdl, B_TRUE, errbuf)) == NULL) 3895e9dbad6fSeschrock return (-1); 38961c10ae76SMike Gerdts if (zfs_fix_auto_resv(zhp, props) == -1) { 38971c10ae76SMike Gerdts nvlist_free(props); 38981c10ae76SMike Gerdts return (-1); 38991c10ae76SMike Gerdts } 3900e9dbad6fSeschrock } 3901e9dbad6fSeschrock 3902eb633035STom Caputi if (zfs_crypto_clone_check(hdl, zhp, parent, props) != 0) { 3903eb633035STom Caputi nvlist_free(props); 3904eb633035STom Caputi return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf)); 3905eb633035STom Caputi } 3906eb633035STom Caputi 39074445fffbSMatthew Ahrens ret = lzc_clone(target, zhp->zfs_name, props); 3908e9dbad6fSeschrock nvlist_free(props); 3909e9dbad6fSeschrock 3910fa9e4066Sahrens if (ret != 0) { 3911fa9e4066Sahrens switch (errno) { 3912fa9e4066Sahrens 3913fa9e4066Sahrens case ENOENT: 3914fa9e4066Sahrens /* 3915fa9e4066Sahrens * The parent doesn't exist. We should have caught this 3916fa9e4066Sahrens * above, but there may a race condition that has since 3917fa9e4066Sahrens * destroyed the parent. 3918fa9e4066Sahrens * 3919fa9e4066Sahrens * At this point, we don't know whether it's the source 3920fa9e4066Sahrens * that doesn't exist anymore, or whether the target 3921fa9e4066Sahrens * dataset doesn't exist. 3922fa9e4066Sahrens */ 392399653d4eSeschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 392499653d4eSeschrock "no such parent '%s'"), parent); 392599653d4eSeschrock return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf)); 3926fa9e4066Sahrens 392799653d4eSeschrock case EXDEV: 392899653d4eSeschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 392999653d4eSeschrock "source and target pools differ")); 393099653d4eSeschrock return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET, 393199653d4eSeschrock errbuf)); 393299653d4eSeschrock 393399653d4eSeschrock default: 393499653d4eSeschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 393599653d4eSeschrock errbuf)); 393699653d4eSeschrock } 393799653d4eSeschrock } 393899653d4eSeschrock 393999653d4eSeschrock return (ret); 394099653d4eSeschrock } 394199653d4eSeschrock 394299653d4eSeschrock /* 394399653d4eSeschrock * Promotes the given clone fs to be the clone parent. 394499653d4eSeschrock */ 394599653d4eSeschrock int 394699653d4eSeschrock zfs_promote(zfs_handle_t *zhp) 394799653d4eSeschrock { 394899653d4eSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 3949a4b8c9aaSAndrew Stormont char snapname[ZFS_MAX_DATASET_NAME_LEN]; 395099653d4eSeschrock int ret; 395199653d4eSeschrock char errbuf[1024]; 395299653d4eSeschrock 395399653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 395499653d4eSeschrock "cannot promote '%s'"), zhp->zfs_name); 395599653d4eSeschrock 395699653d4eSeschrock if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 395799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 395899653d4eSeschrock "snapshots can not be promoted")); 395999653d4eSeschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 396099653d4eSeschrock } 396199653d4eSeschrock 3962a4b8c9aaSAndrew Stormont if (zhp->zfs_dmustats.dds_origin[0] == '\0') { 396399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 396499653d4eSeschrock "not a cloned filesystem")); 396599653d4eSeschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 396699653d4eSeschrock } 396799653d4eSeschrock 3968add927f8Sloli10K if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE)) 3969add927f8Sloli10K return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3970add927f8Sloli10K 3971a4b8c9aaSAndrew Stormont ret = lzc_promote(zhp->zfs_name, snapname, sizeof (snapname)); 397299653d4eSeschrock 397399653d4eSeschrock if (ret != 0) { 3974a4b8c9aaSAndrew Stormont switch (ret) { 3975fa9e4066Sahrens case EEXIST: 3976681d9761SEric Taylor /* There is a conflicting snapshot name. */ 397799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3978681d9761SEric Taylor "conflicting snapshot '%s' from parent '%s'"), 3979a4b8c9aaSAndrew Stormont snapname, zhp->zfs_dmustats.dds_origin); 398099653d4eSeschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3981fa9e4066Sahrens 3982fa9e4066Sahrens default: 3983a4b8c9aaSAndrew Stormont return (zfs_standard_error(hdl, ret, errbuf)); 3984fa9e4066Sahrens } 3985fa9e4066Sahrens } 3986e9dbad6fSeschrock return (ret); 39871d452cf5Sahrens } 39881d452cf5Sahrens 39894445fffbSMatthew Ahrens typedef struct snapdata { 39904445fffbSMatthew Ahrens nvlist_t *sd_nvl; 39914445fffbSMatthew Ahrens const char *sd_snapname; 39924445fffbSMatthew Ahrens } snapdata_t; 39934445fffbSMatthew Ahrens 39944445fffbSMatthew Ahrens static int 39954445fffbSMatthew Ahrens zfs_snapshot_cb(zfs_handle_t *zhp, void *arg) 39964445fffbSMatthew Ahrens { 39974445fffbSMatthew Ahrens snapdata_t *sd = arg; 39989adfa60dSMatthew Ahrens char name[ZFS_MAX_DATASET_NAME_LEN]; 39994445fffbSMatthew Ahrens int rv = 0; 40004445fffbSMatthew Ahrens 4001ca48f36fSKeith M Wesolowski if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) { 40024445fffbSMatthew Ahrens (void) snprintf(name, sizeof (name), 40034445fffbSMatthew Ahrens "%s@%s", zfs_get_name(zhp), sd->sd_snapname); 40044445fffbSMatthew Ahrens 40054445fffbSMatthew Ahrens fnvlist_add_boolean(sd->sd_nvl, name); 40064445fffbSMatthew Ahrens 40074445fffbSMatthew Ahrens rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd); 4008ca48f36fSKeith M Wesolowski } 40094445fffbSMatthew Ahrens zfs_close(zhp); 4010ca48f36fSKeith M Wesolowski 40114445fffbSMatthew Ahrens return (rv); 40124445fffbSMatthew Ahrens } 40134445fffbSMatthew Ahrens 40145cabbc6bSPrashanth Sreenivasa int 40155cabbc6bSPrashanth Sreenivasa zfs_remap_indirects(libzfs_handle_t *hdl, const char *fs) 40165cabbc6bSPrashanth Sreenivasa { 40175cabbc6bSPrashanth Sreenivasa int err; 40185cabbc6bSPrashanth Sreenivasa char errbuf[1024]; 40195cabbc6bSPrashanth Sreenivasa 40205cabbc6bSPrashanth Sreenivasa (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 40210b2e8253Sloli10K "cannot remap dataset '%s'"), fs); 40225cabbc6bSPrashanth Sreenivasa 40235cabbc6bSPrashanth Sreenivasa err = lzc_remap(fs); 40245cabbc6bSPrashanth Sreenivasa 40255cabbc6bSPrashanth Sreenivasa if (err != 0) { 40260b2e8253Sloli10K switch (err) { 40270b2e8253Sloli10K case ENOTSUP: 40280b2e8253Sloli10K zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 40290b2e8253Sloli10K "pool must be upgraded")); 40300b2e8253Sloli10K (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 40310b2e8253Sloli10K break; 40320b2e8253Sloli10K case EINVAL: 40330b2e8253Sloli10K (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 40340b2e8253Sloli10K break; 40350b2e8253Sloli10K default: 40365cabbc6bSPrashanth Sreenivasa (void) zfs_standard_error(hdl, err, errbuf); 40370b2e8253Sloli10K break; 40380b2e8253Sloli10K } 40395cabbc6bSPrashanth Sreenivasa } 40405cabbc6bSPrashanth Sreenivasa 40415cabbc6bSPrashanth Sreenivasa return (err); 40425cabbc6bSPrashanth Sreenivasa } 40435cabbc6bSPrashanth Sreenivasa 4044fa9e4066Sahrens /* 40454445fffbSMatthew Ahrens * Creates snapshots. The keys in the snaps nvlist are the snapshots to be 40464445fffbSMatthew Ahrens * created. 4047fa9e4066Sahrens */ 4048fa9e4066Sahrens int 40494445fffbSMatthew Ahrens zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props) 40504445fffbSMatthew Ahrens { 40514445fffbSMatthew Ahrens int ret; 40524445fffbSMatthew Ahrens char errbuf[1024]; 40534445fffbSMatthew Ahrens nvpair_t *elem; 40544445fffbSMatthew Ahrens nvlist_t *errors; 40554445fffbSMatthew Ahrens 40564445fffbSMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 40574445fffbSMatthew Ahrens "cannot create snapshots ")); 40584445fffbSMatthew Ahrens 40594445fffbSMatthew Ahrens elem = NULL; 40604445fffbSMatthew Ahrens while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) { 40614445fffbSMatthew Ahrens const char *snapname = nvpair_name(elem); 40624445fffbSMatthew Ahrens 40634445fffbSMatthew Ahrens /* validate the target name */ 40644445fffbSMatthew Ahrens if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT, 40654445fffbSMatthew Ahrens B_TRUE)) { 40664445fffbSMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), 40674445fffbSMatthew Ahrens dgettext(TEXT_DOMAIN, 40684445fffbSMatthew Ahrens "cannot create snapshot '%s'"), snapname); 40694445fffbSMatthew Ahrens return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 40704445fffbSMatthew Ahrens } 40714445fffbSMatthew Ahrens } 40724445fffbSMatthew Ahrens 4073e9316f76SJoe Stein /* 4074e9316f76SJoe Stein * get pool handle for prop validation. assumes all snaps are in the 4075e9316f76SJoe Stein * same pool, as does lzc_snapshot (below). 4076e9316f76SJoe Stein */ 40779adfa60dSMatthew Ahrens char pool[ZFS_MAX_DATASET_NAME_LEN]; 4078e9316f76SJoe Stein elem = nvlist_next_nvpair(snaps, NULL); 4079e9316f76SJoe Stein (void) strlcpy(pool, nvpair_name(elem), sizeof (pool)); 4080e9316f76SJoe Stein pool[strcspn(pool, "/@")] = '\0'; 4081e9316f76SJoe Stein zpool_handle_t *zpool_hdl = zpool_open(hdl, pool); 4082e9316f76SJoe Stein 40834445fffbSMatthew Ahrens if (props != NULL && 40844445fffbSMatthew Ahrens (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT, 4085eb633035STom Caputi props, B_FALSE, NULL, zpool_hdl, B_FALSE, errbuf)) == NULL) { 4086e9316f76SJoe Stein zpool_close(zpool_hdl); 40874445fffbSMatthew Ahrens return (-1); 40884445fffbSMatthew Ahrens } 4089e9316f76SJoe Stein zpool_close(zpool_hdl); 40904445fffbSMatthew Ahrens 40914445fffbSMatthew Ahrens ret = lzc_snapshot(snaps, props, &errors); 40924445fffbSMatthew Ahrens 40934445fffbSMatthew Ahrens if (ret != 0) { 40944445fffbSMatthew Ahrens boolean_t printed = B_FALSE; 40954445fffbSMatthew Ahrens for (elem = nvlist_next_nvpair(errors, NULL); 40964445fffbSMatthew Ahrens elem != NULL; 40974445fffbSMatthew Ahrens elem = nvlist_next_nvpair(errors, elem)) { 40984445fffbSMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), 40994445fffbSMatthew Ahrens dgettext(TEXT_DOMAIN, 41004445fffbSMatthew Ahrens "cannot create snapshot '%s'"), nvpair_name(elem)); 41014445fffbSMatthew Ahrens (void) zfs_standard_error(hdl, 41024445fffbSMatthew Ahrens fnvpair_value_int32(elem), errbuf); 41034445fffbSMatthew Ahrens printed = B_TRUE; 41044445fffbSMatthew Ahrens } 41054445fffbSMatthew Ahrens if (!printed) { 41064445fffbSMatthew Ahrens switch (ret) { 41074445fffbSMatthew Ahrens case EXDEV: 41084445fffbSMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 41094445fffbSMatthew Ahrens "multiple snapshots of same " 41104445fffbSMatthew Ahrens "fs not allowed")); 41114445fffbSMatthew Ahrens (void) zfs_error(hdl, EZFS_EXISTS, errbuf); 41124445fffbSMatthew Ahrens 41134445fffbSMatthew Ahrens break; 41144445fffbSMatthew Ahrens default: 41154445fffbSMatthew Ahrens (void) zfs_standard_error(hdl, ret, errbuf); 41164445fffbSMatthew Ahrens } 41174445fffbSMatthew Ahrens } 41184445fffbSMatthew Ahrens } 41194445fffbSMatthew Ahrens 41204445fffbSMatthew Ahrens nvlist_free(props); 41214445fffbSMatthew Ahrens nvlist_free(errors); 41224445fffbSMatthew Ahrens return (ret); 41234445fffbSMatthew Ahrens } 41244445fffbSMatthew Ahrens 41254445fffbSMatthew Ahrens int 4126bb0ade09Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive, 4127bb0ade09Sahrens nvlist_t *props) 4128fa9e4066Sahrens { 4129fa9e4066Sahrens int ret; 41304445fffbSMatthew Ahrens snapdata_t sd = { 0 }; 41319adfa60dSMatthew Ahrens char fsname[ZFS_MAX_DATASET_NAME_LEN]; 41324445fffbSMatthew Ahrens char *cp; 41334445fffbSMatthew Ahrens zfs_handle_t *zhp; 413499653d4eSeschrock char errbuf[1024]; 4135fa9e4066Sahrens 413699653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 41374445fffbSMatthew Ahrens "cannot snapshot %s"), path); 413899653d4eSeschrock 4139f18faf3fSek110237 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE)) 414099653d4eSeschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 4141fa9e4066Sahrens 41424445fffbSMatthew Ahrens (void) strlcpy(fsname, path, sizeof (fsname)); 41434445fffbSMatthew Ahrens cp = strchr(fsname, '@'); 41444445fffbSMatthew Ahrens *cp = '\0'; 41454445fffbSMatthew Ahrens sd.sd_snapname = cp + 1; 4146bb0ade09Sahrens 41474445fffbSMatthew Ahrens if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | 4148fa9e4066Sahrens ZFS_TYPE_VOLUME)) == NULL) { 4149fa9e4066Sahrens return (-1); 4150fa9e4066Sahrens } 4151fa9e4066Sahrens 41524445fffbSMatthew Ahrens verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0); 41534445fffbSMatthew Ahrens if (recursive) { 41544445fffbSMatthew Ahrens (void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd); 41554445fffbSMatthew Ahrens } else { 41564445fffbSMatthew Ahrens fnvlist_add_boolean(sd.sd_nvl, path); 4157681d9761SEric Taylor } 4158fa9e4066Sahrens 41594445fffbSMatthew Ahrens ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props); 41604445fffbSMatthew Ahrens nvlist_free(sd.sd_nvl); 4161fa9e4066Sahrens zfs_close(zhp); 4162fa9e4066Sahrens return (ret); 4163fa9e4066Sahrens } 4164fa9e4066Sahrens 4165fa9e4066Sahrens /* 4166b12a1c38Slling * Destroy any more recent snapshots. We invoke this callback on any dependents 4167b12a1c38Slling * of the snapshot first. If the 'cb_dependent' member is non-zero, then this 4168b12a1c38Slling * is a dependent and we should just destroy it without checking the transaction 4169b12a1c38Slling * group. 4170fa9e4066Sahrens */ 4171b12a1c38Slling typedef struct rollback_data { 4172b12a1c38Slling const char *cb_target; /* the snapshot */ 4173b12a1c38Slling uint64_t cb_create; /* creation time reference */ 4174c391e322Sahrens boolean_t cb_error; 4175c391e322Sahrens boolean_t cb_force; 4176b12a1c38Slling } rollback_data_t; 4177b12a1c38Slling 4178b12a1c38Slling static int 417978f17100SMatthew Ahrens rollback_destroy_dependent(zfs_handle_t *zhp, void *data) 4180b12a1c38Slling { 4181b12a1c38Slling rollback_data_t *cbp = data; 4182c391e322Sahrens prop_changelist_t *clp; 4183c391e322Sahrens 418478f17100SMatthew Ahrens /* We must destroy this clone; first unmount it */ 41850069fd67STim Haley clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 4186c391e322Sahrens cbp->cb_force ? MS_FORCE: 0); 4187c391e322Sahrens if (clp == NULL || changelist_prefix(clp) != 0) { 4188c391e322Sahrens cbp->cb_error = B_TRUE; 4189c391e322Sahrens zfs_close(zhp); 4190c391e322Sahrens return (0); 4191c391e322Sahrens } 4192842727c2SChris Kirby if (zfs_destroy(zhp, B_FALSE) != 0) 4193c391e322Sahrens cbp->cb_error = B_TRUE; 4194c391e322Sahrens else 4195c391e322Sahrens changelist_remove(clp, zhp->zfs_name); 4196ba7b046eSahrens (void) changelist_postfix(clp); 4197c391e322Sahrens changelist_free(clp); 419878f17100SMatthew Ahrens 419978f17100SMatthew Ahrens zfs_close(zhp); 420078f17100SMatthew Ahrens return (0); 420178f17100SMatthew Ahrens } 420278f17100SMatthew Ahrens 420378f17100SMatthew Ahrens static int 420478f17100SMatthew Ahrens rollback_destroy(zfs_handle_t *zhp, void *data) 420578f17100SMatthew Ahrens { 420678f17100SMatthew Ahrens rollback_data_t *cbp = data; 420778f17100SMatthew Ahrens 420878f17100SMatthew Ahrens if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) { 420978f17100SMatthew Ahrens cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE, 421078f17100SMatthew Ahrens rollback_destroy_dependent, cbp); 421178f17100SMatthew Ahrens 421278f17100SMatthew Ahrens cbp->cb_error |= zfs_destroy(zhp, B_FALSE); 4213b12a1c38Slling } 4214b12a1c38Slling 4215b12a1c38Slling zfs_close(zhp); 4216b12a1c38Slling return (0); 4217b12a1c38Slling } 4218b12a1c38Slling 4219b12a1c38Slling /* 42204ccbb6e7Sahrens * Given a dataset, rollback to a specific snapshot, discarding any 42214ccbb6e7Sahrens * data changes since then and making it the active dataset. 42224ccbb6e7Sahrens * 422378f17100SMatthew Ahrens * Any snapshots and bookmarks more recent than the target are 422478f17100SMatthew Ahrens * destroyed, along with their dependents (i.e. clones). 4225b12a1c38Slling */ 42264ccbb6e7Sahrens int 4227c391e322Sahrens zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force) 4228fa9e4066Sahrens { 42294ccbb6e7Sahrens rollback_data_t cb = { 0 }; 42304ccbb6e7Sahrens int err; 42317b97dc1aSrm160521 boolean_t restore_resv = 0; 4232f83b46baSPaul Dagnelie uint64_t old_volsize = 0, new_volsize; 42337b97dc1aSrm160521 zfs_prop_t resv_prop; 4234fa9e4066Sahrens 4235fa9e4066Sahrens assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM || 4236fa9e4066Sahrens zhp->zfs_type == ZFS_TYPE_VOLUME); 4237fa9e4066Sahrens 42384ccbb6e7Sahrens /* 42392e2c1355SMatthew Ahrens * Destroy all recent snapshots and their dependents. 42404ccbb6e7Sahrens */ 4241c391e322Sahrens cb.cb_force = force; 42424ccbb6e7Sahrens cb.cb_target = snap->zfs_name; 42434ccbb6e7Sahrens cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG); 42440d8fa8f8SMartin Matuska (void) zfs_iter_snapshots(zhp, B_FALSE, rollback_destroy, &cb); 424578f17100SMatthew Ahrens (void) zfs_iter_bookmarks(zhp, rollback_destroy, &cb); 42464ccbb6e7Sahrens 4247c391e322Sahrens if (cb.cb_error) 4248c391e322Sahrens return (-1); 42494ccbb6e7Sahrens 42504ccbb6e7Sahrens /* 42514ccbb6e7Sahrens * Now that we have verified that the snapshot is the latest, 42524ccbb6e7Sahrens * rollback to the given snapshot. 42534ccbb6e7Sahrens */ 42544ccbb6e7Sahrens 42557b97dc1aSrm160521 if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 42567b97dc1aSrm160521 if (zfs_which_resv_prop(zhp, &resv_prop) < 0) 42577b97dc1aSrm160521 return (-1); 42587b97dc1aSrm160521 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 42597b97dc1aSrm160521 restore_resv = 42607b97dc1aSrm160521 (old_volsize == zfs_prop_get_int(zhp, resv_prop)); 42617b97dc1aSrm160521 } 4262fa9e4066Sahrens 4263fa9e4066Sahrens /* 426477b17137SAndriy Gapon * Pass both the filesystem and the wanted snapshot names, 426577b17137SAndriy Gapon * we would get an error back if the snapshot is destroyed or 426677b17137SAndriy Gapon * a new snapshot is created before this request is processed. 4267fa9e4066Sahrens */ 426877b17137SAndriy Gapon err = lzc_rollback_to(zhp->zfs_name, snap->zfs_name); 426995643f75SAndriy Gapon if (err != 0) { 427095643f75SAndriy Gapon char errbuf[1024]; 427195643f75SAndriy Gapon 427295643f75SAndriy Gapon (void) snprintf(errbuf, sizeof (errbuf), 427395643f75SAndriy Gapon dgettext(TEXT_DOMAIN, "cannot rollback '%s'"), 427495643f75SAndriy Gapon zhp->zfs_name); 427595643f75SAndriy Gapon switch (err) { 427695643f75SAndriy Gapon case EEXIST: 427777b17137SAndriy Gapon zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 427895643f75SAndriy Gapon "there is a snapshot or bookmark more recent " 427995643f75SAndriy Gapon "than '%s'"), snap->zfs_name); 428095643f75SAndriy Gapon (void) zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf); 428195643f75SAndriy Gapon break; 428295643f75SAndriy Gapon case ESRCH: 428395643f75SAndriy Gapon zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 428495643f75SAndriy Gapon "'%s' is not found among snapshots of '%s'"), 428595643f75SAndriy Gapon snap->zfs_name, zhp->zfs_name); 428695643f75SAndriy Gapon (void) zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf); 428795643f75SAndriy Gapon break; 428895643f75SAndriy Gapon case EINVAL: 428995643f75SAndriy Gapon (void) zfs_error(zhp->zfs_hdl, EZFS_BADTYPE, errbuf); 429095643f75SAndriy Gapon break; 429195643f75SAndriy Gapon default: 429295643f75SAndriy Gapon (void) zfs_standard_error(zhp->zfs_hdl, err, errbuf); 429395643f75SAndriy Gapon } 4294b9415e83Srm160521 return (err); 4295b9415e83Srm160521 } 4296fa9e4066Sahrens 42977b97dc1aSrm160521 /* 42987b97dc1aSrm160521 * For volumes, if the pre-rollback volsize matched the pre- 42997b97dc1aSrm160521 * rollback reservation and the volsize has changed then set 43007b97dc1aSrm160521 * the reservation property to the post-rollback volsize. 43017b97dc1aSrm160521 * Make a new handle since the rollback closed the dataset. 43027b97dc1aSrm160521 */ 4303b9415e83Srm160521 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) && 4304b9415e83Srm160521 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) { 43057b97dc1aSrm160521 if (restore_resv) { 43067b97dc1aSrm160521 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 43077b97dc1aSrm160521 if (old_volsize != new_volsize) 4308b9415e83Srm160521 err = zfs_prop_set_int(zhp, resv_prop, 4309b9415e83Srm160521 new_volsize); 43107b97dc1aSrm160521 } 43117b97dc1aSrm160521 zfs_close(zhp); 43127b97dc1aSrm160521 } 43134ccbb6e7Sahrens return (err); 4314b12a1c38Slling } 4315b12a1c38Slling 4316b12a1c38Slling /* 4317fa9e4066Sahrens * Renames the given dataset. 4318fa9e4066Sahrens */ 4319fa9e4066Sahrens int 43206a9cb0eaSEric Schrock zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive, 43216a9cb0eaSEric Schrock boolean_t force_unmount) 4322fa9e4066Sahrens { 432388f61deeSIgor Kozhukhov int ret = 0; 4324fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 4325fa9e4066Sahrens char *delim; 4326cdf5b4caSmmusante prop_changelist_t *cl = NULL; 4327cdf5b4caSmmusante zfs_handle_t *zhrp = NULL; 4328cdf5b4caSmmusante char *parentname = NULL; 43299adfa60dSMatthew Ahrens char parent[ZFS_MAX_DATASET_NAME_LEN]; 433099653d4eSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 433199653d4eSeschrock char errbuf[1024]; 4332fa9e4066Sahrens 4333fa9e4066Sahrens /* if we have the same exact name, just return success */ 4334fa9e4066Sahrens if (strcmp(zhp->zfs_name, target) == 0) 4335fa9e4066Sahrens return (0); 4336fa9e4066Sahrens 433799653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 433899653d4eSeschrock "cannot rename to '%s'"), target); 433999653d4eSeschrock 4340add927f8Sloli10K /* make sure source name is valid */ 4341add927f8Sloli10K if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE)) 4342add927f8Sloli10K return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 4343add927f8Sloli10K 4344fa9e4066Sahrens /* 4345fa9e4066Sahrens * Make sure the target name is valid 4346fa9e4066Sahrens */ 4347fa9e4066Sahrens if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 434898579b20Snd150628 if ((strchr(target, '@') == NULL) || 434998579b20Snd150628 *target == '@') { 435098579b20Snd150628 /* 435198579b20Snd150628 * Snapshot target name is abbreviated, 435298579b20Snd150628 * reconstruct full dataset name 435398579b20Snd150628 */ 435498579b20Snd150628 (void) strlcpy(parent, zhp->zfs_name, 435598579b20Snd150628 sizeof (parent)); 435698579b20Snd150628 delim = strchr(parent, '@'); 435798579b20Snd150628 if (strchr(target, '@') == NULL) 435898579b20Snd150628 *(++delim) = '\0'; 435998579b20Snd150628 else 436098579b20Snd150628 *delim = '\0'; 436198579b20Snd150628 (void) strlcat(parent, target, sizeof (parent)); 436298579b20Snd150628 target = parent; 436398579b20Snd150628 } else { 4364fa9e4066Sahrens /* 4365fa9e4066Sahrens * Make sure we're renaming within the same dataset. 4366fa9e4066Sahrens */ 436798579b20Snd150628 delim = strchr(target, '@'); 436898579b20Snd150628 if (strncmp(zhp->zfs_name, target, delim - target) 436998579b20Snd150628 != 0 || zhp->zfs_name[delim - target] != '@') { 437099653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 437198579b20Snd150628 "snapshots must be part of same " 437298579b20Snd150628 "dataset")); 437398579b20Snd150628 return (zfs_error(hdl, EZFS_CROSSTARGET, 437498579b20Snd150628 errbuf)); 4375fa9e4066Sahrens } 437698579b20Snd150628 } 43775ac95da7SSerapheim Dimitropoulos 4378f18faf3fSek110237 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 437998579b20Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 4380fa9e4066Sahrens } else { 4381cdf5b4caSmmusante if (recursive) { 4382cdf5b4caSmmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4383cdf5b4caSmmusante "recursive rename must be a snapshot")); 4384cdf5b4caSmmusante return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 4385cdf5b4caSmmusante } 4386cdf5b4caSmmusante 4387f18faf3fSek110237 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 438898579b20Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 4389e9dbad6fSeschrock 4390fa9e4066Sahrens /* validate parents */ 4391d41c4376SMark J Musante if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0) 4392fa9e4066Sahrens return (-1); 4393fa9e4066Sahrens 4394fa9e4066Sahrens /* make sure we're in the same pool */ 4395fa9e4066Sahrens verify((delim = strchr(target, '/')) != NULL); 4396fa9e4066Sahrens if (strncmp(zhp->zfs_name, target, delim - target) != 0 || 4397fa9e4066Sahrens zhp->zfs_name[delim - target] != '/') { 439899653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 439999653d4eSeschrock "datasets must be within same pool")); 440099653d4eSeschrock return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 4401fa9e4066Sahrens } 4402f2fdf992Snd150628 4403f2fdf992Snd150628 /* new name cannot be a child of the current dataset name */ 4404d41c4376SMark J Musante if (is_descendant(zhp->zfs_name, target)) { 4405f2fdf992Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4406d41c4376SMark J Musante "New dataset name cannot be a descendant of " 4407f2fdf992Snd150628 "current dataset name")); 4408f2fdf992Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 4409f2fdf992Snd150628 } 4410fa9e4066Sahrens } 4411fa9e4066Sahrens 441299653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), 441399653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name); 441499653d4eSeschrock 4415fa9e4066Sahrens if (getzoneid() == GLOBAL_ZONEID && 4416fa9e4066Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 441799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 441899653d4eSeschrock "dataset is used in a non-global zone")); 441999653d4eSeschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 4420fa9e4066Sahrens } 4421fa9e4066Sahrens 4422cdf5b4caSmmusante if (recursive) { 4423f0c5ee21Smmusante parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name); 4424f0c5ee21Smmusante if (parentname == NULL) { 4425f0c5ee21Smmusante ret = -1; 4426f0c5ee21Smmusante goto error; 4427f0c5ee21Smmusante } 4428cdf5b4caSmmusante delim = strchr(parentname, '@'); 4429cdf5b4caSmmusante *delim = '\0'; 4430990b4856Slling zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET); 4431cdf5b4caSmmusante if (zhrp == NULL) { 4432f0c5ee21Smmusante ret = -1; 4433f0c5ee21Smmusante goto error; 4434cdf5b4caSmmusante } 443533cde0d0SMatthew Ahrens } else if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) { 44366a9cb0eaSEric Schrock if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0, 44376a9cb0eaSEric Schrock force_unmount ? MS_FORCE : 0)) == NULL) 443899653d4eSeschrock return (-1); 4439fa9e4066Sahrens 4440fa9e4066Sahrens if (changelist_haszonedchild(cl)) { 444199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 444299653d4eSeschrock "child dataset with inherited mountpoint is used " 444399653d4eSeschrock "in a non-global zone")); 4444e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_ZONED, errbuf); 4445f83b46baSPaul Dagnelie ret = -1; 4446fa9e4066Sahrens goto error; 4447fa9e4066Sahrens } 4448fa9e4066Sahrens 4449fa9e4066Sahrens if ((ret = changelist_prefix(cl)) != 0) 4450fa9e4066Sahrens goto error; 4451cdf5b4caSmmusante } 4452fa9e4066Sahrens 4453e9dbad6fSeschrock if (ZFS_IS_VOLUME(zhp)) 4454fa9e4066Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 4455fa9e4066Sahrens else 4456fa9e4066Sahrens zc.zc_objset_type = DMU_OST_ZFS; 4457fa9e4066Sahrens 445898579b20Snd150628 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 4459e9dbad6fSeschrock (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value)); 446098579b20Snd150628 4461cdf5b4caSmmusante zc.zc_cookie = recursive; 4462cdf5b4caSmmusante 4463ecd6cf80Smarks if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) { 4464cdf5b4caSmmusante /* 4465cdf5b4caSmmusante * if it was recursive, the one that actually failed will 4466cdf5b4caSmmusante * be in zc.zc_name 4467cdf5b4caSmmusante */ 4468cdf5b4caSmmusante (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 44693cb34c60Sahrens "cannot rename '%s'"), zc.zc_name); 4470cdf5b4caSmmusante 4471cdf5b4caSmmusante if (recursive && errno == EEXIST) { 4472cdf5b4caSmmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4473cdf5b4caSmmusante "a child dataset already has a snapshot " 4474cdf5b4caSmmusante "with the new name")); 4475a10acbd6Seschrock (void) zfs_error(hdl, EZFS_EXISTS, errbuf); 4476eb633035STom Caputi } else if (errno == EACCES) { 4477eb633035STom Caputi zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4478a60ca23dSTom Caputi "cannot move encrypted child outside of " 4479eb633035STom Caputi "its encryption root")); 4480eb633035STom Caputi (void) zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf); 4481cdf5b4caSmmusante } else { 448299653d4eSeschrock (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf); 4483cdf5b4caSmmusante } 4484fa9e4066Sahrens 4485fa9e4066Sahrens /* 4486fa9e4066Sahrens * On failure, we still want to remount any filesystems that 4487fa9e4066Sahrens * were previously mounted, so we don't alter the system state. 4488fa9e4066Sahrens */ 448933cde0d0SMatthew Ahrens if (cl != NULL) 4490fa9e4066Sahrens (void) changelist_postfix(cl); 4491cdf5b4caSmmusante } else { 449233cde0d0SMatthew Ahrens if (cl != NULL) { 4493fa9e4066Sahrens changelist_rename(cl, zfs_get_name(zhp), target); 4494fa9e4066Sahrens ret = changelist_postfix(cl); 4495fa9e4066Sahrens } 4496cdf5b4caSmmusante } 4497fa9e4066Sahrens 4498fa9e4066Sahrens error: 449933cde0d0SMatthew Ahrens if (parentname != NULL) { 4500cdf5b4caSmmusante free(parentname); 4501cdf5b4caSmmusante } 450233cde0d0SMatthew Ahrens if (zhrp != NULL) { 4503cdf5b4caSmmusante zfs_close(zhrp); 4504cdf5b4caSmmusante } 450533cde0d0SMatthew Ahrens if (cl != NULL) { 4506fa9e4066Sahrens changelist_free(cl); 4507cdf5b4caSmmusante } 4508fa9e4066Sahrens return (ret); 4509fa9e4066Sahrens } 4510fa9e4066Sahrens 4511e9dbad6fSeschrock nvlist_t * 4512e9dbad6fSeschrock zfs_get_user_props(zfs_handle_t *zhp) 4513e9dbad6fSeschrock { 4514e9dbad6fSeschrock return (zhp->zfs_user_props); 4515e9dbad6fSeschrock } 4516e9dbad6fSeschrock 451792241e0bSTom Erickson nvlist_t * 451892241e0bSTom Erickson zfs_get_recvd_props(zfs_handle_t *zhp) 451992241e0bSTom Erickson { 452092241e0bSTom Erickson if (zhp->zfs_recvd_props == NULL) 452192241e0bSTom Erickson if (get_recvd_props_ioctl(zhp) != 0) 452292241e0bSTom Erickson return (NULL); 452392241e0bSTom Erickson return (zhp->zfs_recvd_props); 452492241e0bSTom Erickson } 452592241e0bSTom Erickson 4526e9dbad6fSeschrock /* 4527e9dbad6fSeschrock * This function is used by 'zfs list' to determine the exact set of columns to 4528e9dbad6fSeschrock * display, and their maximum widths. This does two main things: 4529e9dbad6fSeschrock * 4530e9dbad6fSeschrock * - If this is a list of all properties, then expand the list to include 4531e9dbad6fSeschrock * all native properties, and set a flag so that for each dataset we look 4532e9dbad6fSeschrock * for new unique user properties and add them to the list. 4533e9dbad6fSeschrock * 4534e9dbad6fSeschrock * - For non fixed-width properties, keep track of the maximum width seen 453592241e0bSTom Erickson * so that we can size the column appropriately. If the user has 453692241e0bSTom Erickson * requested received property values, we also need to compute the width 453792241e0bSTom Erickson * of the RECEIVED column. 4538e9dbad6fSeschrock */ 4539e9dbad6fSeschrock int 454043d68d68SYuri Pankov zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received, 454143d68d68SYuri Pankov boolean_t literal) 4542e9dbad6fSeschrock { 4543e9dbad6fSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 4544990b4856Slling zprop_list_t *entry; 4545990b4856Slling zprop_list_t **last, **start; 4546e9dbad6fSeschrock nvlist_t *userprops, *propval; 4547e9dbad6fSeschrock nvpair_t *elem; 4548e9dbad6fSeschrock char *strval; 4549e9dbad6fSeschrock char buf[ZFS_MAXPROPLEN]; 4550e9dbad6fSeschrock 4551990b4856Slling if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0) 4552e9dbad6fSeschrock return (-1); 4553e9dbad6fSeschrock 4554e9dbad6fSeschrock userprops = zfs_get_user_props(zhp); 4555e9dbad6fSeschrock 4556e9dbad6fSeschrock entry = *plp; 4557e9dbad6fSeschrock if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) { 4558e9dbad6fSeschrock /* 4559e9dbad6fSeschrock * Go through and add any user properties as necessary. We 4560e9dbad6fSeschrock * start by incrementing our list pointer to the first 4561e9dbad6fSeschrock * non-native property. 4562e9dbad6fSeschrock */ 4563e9dbad6fSeschrock start = plp; 4564e9dbad6fSeschrock while (*start != NULL) { 4565990b4856Slling if ((*start)->pl_prop == ZPROP_INVAL) 4566e9dbad6fSeschrock break; 4567e9dbad6fSeschrock start = &(*start)->pl_next; 4568e9dbad6fSeschrock } 4569e9dbad6fSeschrock 4570e9dbad6fSeschrock elem = NULL; 4571e9dbad6fSeschrock while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) { 4572e9dbad6fSeschrock /* 4573e9dbad6fSeschrock * See if we've already found this property in our list. 4574e9dbad6fSeschrock */ 4575e9dbad6fSeschrock for (last = start; *last != NULL; 4576e9dbad6fSeschrock last = &(*last)->pl_next) { 4577e9dbad6fSeschrock if (strcmp((*last)->pl_user_prop, 4578e9dbad6fSeschrock nvpair_name(elem)) == 0) 4579e9dbad6fSeschrock break; 4580e9dbad6fSeschrock } 4581e9dbad6fSeschrock 4582e9dbad6fSeschrock if (*last == NULL) { 4583e9dbad6fSeschrock if ((entry = zfs_alloc(hdl, 4584990b4856Slling sizeof (zprop_list_t))) == NULL || 4585e9dbad6fSeschrock ((entry->pl_user_prop = zfs_strdup(hdl, 4586e9dbad6fSeschrock nvpair_name(elem)))) == NULL) { 4587e9dbad6fSeschrock free(entry); 4588e9dbad6fSeschrock return (-1); 4589e9dbad6fSeschrock } 4590e9dbad6fSeschrock 4591990b4856Slling entry->pl_prop = ZPROP_INVAL; 4592e9dbad6fSeschrock entry->pl_width = strlen(nvpair_name(elem)); 4593e9dbad6fSeschrock entry->pl_all = B_TRUE; 4594e9dbad6fSeschrock *last = entry; 4595e9dbad6fSeschrock } 4596e9dbad6fSeschrock } 4597e9dbad6fSeschrock } 4598e9dbad6fSeschrock 4599e9dbad6fSeschrock /* 4600e9dbad6fSeschrock * Now go through and check the width of any non-fixed columns 4601e9dbad6fSeschrock */ 4602e9dbad6fSeschrock for (entry = *plp; entry != NULL; entry = entry->pl_next) { 460343d68d68SYuri Pankov if (entry->pl_fixed && !literal) 4604e9dbad6fSeschrock continue; 4605e9dbad6fSeschrock 4606990b4856Slling if (entry->pl_prop != ZPROP_INVAL) { 4607e9dbad6fSeschrock if (zfs_prop_get(zhp, entry->pl_prop, 460843d68d68SYuri Pankov buf, sizeof (buf), NULL, NULL, 0, literal) == 0) { 4609e9dbad6fSeschrock if (strlen(buf) > entry->pl_width) 4610e9dbad6fSeschrock entry->pl_width = strlen(buf); 4611e9dbad6fSeschrock } 461292241e0bSTom Erickson if (received && zfs_prop_get_recvd(zhp, 461392241e0bSTom Erickson zfs_prop_to_name(entry->pl_prop), 461443d68d68SYuri Pankov buf, sizeof (buf), literal) == 0) 461592241e0bSTom Erickson if (strlen(buf) > entry->pl_recvd_width) 461692241e0bSTom Erickson entry->pl_recvd_width = strlen(buf); 461792241e0bSTom Erickson } else { 461892241e0bSTom Erickson if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop, 461992241e0bSTom Erickson &propval) == 0) { 4620e9dbad6fSeschrock verify(nvlist_lookup_string(propval, 4621990b4856Slling ZPROP_VALUE, &strval) == 0); 4622e9dbad6fSeschrock if (strlen(strval) > entry->pl_width) 4623e9dbad6fSeschrock entry->pl_width = strlen(strval); 4624e9dbad6fSeschrock } 462592241e0bSTom Erickson if (received && zfs_prop_get_recvd(zhp, 462692241e0bSTom Erickson entry->pl_user_prop, 462743d68d68SYuri Pankov buf, sizeof (buf), literal) == 0) 462892241e0bSTom Erickson if (strlen(buf) > entry->pl_recvd_width) 462992241e0bSTom Erickson entry->pl_recvd_width = strlen(buf); 463092241e0bSTom Erickson } 4631e9dbad6fSeschrock } 4632e9dbad6fSeschrock 4633e9dbad6fSeschrock return (0); 4634e9dbad6fSeschrock } 4635ecd6cf80Smarks 4636ecd6cf80Smarks int 4637ecd6cf80Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path, 4638743a77edSAlan Wright char *resource, void *export, void *sharetab, 4639743a77edSAlan Wright int sharemax, zfs_share_op_t operation) 4640ecd6cf80Smarks { 4641ecd6cf80Smarks zfs_cmd_t zc = { 0 }; 4642ecd6cf80Smarks int error; 4643ecd6cf80Smarks 4644ecd6cf80Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 4645ecd6cf80Smarks (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value)); 4646743a77edSAlan Wright if (resource) 4647743a77edSAlan Wright (void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string)); 4648ecd6cf80Smarks zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab; 4649ecd6cf80Smarks zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export; 4650da6c28aaSamw zc.zc_share.z_sharetype = operation; 4651ecd6cf80Smarks zc.zc_share.z_sharemax = sharemax; 4652ecd6cf80Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc); 4653ecd6cf80Smarks return (error); 4654ecd6cf80Smarks } 46552e5e9e19SSanjeev Bagewadi 46562e5e9e19SSanjeev Bagewadi void 46572e5e9e19SSanjeev Bagewadi zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props) 46582e5e9e19SSanjeev Bagewadi { 46592e5e9e19SSanjeev Bagewadi nvpair_t *curr; 46602e5e9e19SSanjeev Bagewadi 46612e5e9e19SSanjeev Bagewadi /* 46622e5e9e19SSanjeev Bagewadi * Keep a reference to the props-table against which we prune the 46632e5e9e19SSanjeev Bagewadi * properties. 46642e5e9e19SSanjeev Bagewadi */ 46652e5e9e19SSanjeev Bagewadi zhp->zfs_props_table = props; 46662e5e9e19SSanjeev Bagewadi 46672e5e9e19SSanjeev Bagewadi curr = nvlist_next_nvpair(zhp->zfs_props, NULL); 46682e5e9e19SSanjeev Bagewadi 46692e5e9e19SSanjeev Bagewadi while (curr) { 46702e5e9e19SSanjeev Bagewadi zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr)); 46712e5e9e19SSanjeev Bagewadi nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr); 46722e5e9e19SSanjeev Bagewadi 467314843421SMatthew Ahrens /* 4674faaa6415SEric Schrock * User properties will result in ZPROP_INVAL, and since we 4675faaa6415SEric Schrock * only know how to prune standard ZFS properties, we always 4676faaa6415SEric Schrock * leave these in the list. This can also happen if we 4677faaa6415SEric Schrock * encounter an unknown DSL property (when running older 4678faaa6415SEric Schrock * software, for example). 467914843421SMatthew Ahrens */ 468014843421SMatthew Ahrens if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE) 46812e5e9e19SSanjeev Bagewadi (void) nvlist_remove(zhp->zfs_props, 46822e5e9e19SSanjeev Bagewadi nvpair_name(curr), nvpair_type(curr)); 46832e5e9e19SSanjeev Bagewadi curr = next; 46842e5e9e19SSanjeev Bagewadi } 46852e5e9e19SSanjeev Bagewadi } 4686743a77edSAlan Wright 4687743a77edSAlan Wright static int 4688743a77edSAlan Wright zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path, 4689743a77edSAlan Wright zfs_smb_acl_op_t cmd, char *resource1, char *resource2) 4690743a77edSAlan Wright { 4691743a77edSAlan Wright zfs_cmd_t zc = { 0 }; 4692743a77edSAlan Wright nvlist_t *nvlist = NULL; 4693743a77edSAlan Wright int error; 4694743a77edSAlan Wright 4695743a77edSAlan Wright (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 4696743a77edSAlan Wright (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value)); 4697743a77edSAlan Wright zc.zc_cookie = (uint64_t)cmd; 4698743a77edSAlan Wright 4699743a77edSAlan Wright if (cmd == ZFS_SMB_ACL_RENAME) { 4700743a77edSAlan Wright if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) { 4701743a77edSAlan Wright (void) no_memory(hdl); 470230925561SChris Williamson return (0); 4703743a77edSAlan Wright } 4704743a77edSAlan Wright } 4705743a77edSAlan Wright 4706743a77edSAlan Wright switch (cmd) { 4707743a77edSAlan Wright case ZFS_SMB_ACL_ADD: 4708743a77edSAlan Wright case ZFS_SMB_ACL_REMOVE: 4709743a77edSAlan Wright (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string)); 4710743a77edSAlan Wright break; 4711743a77edSAlan Wright case ZFS_SMB_ACL_RENAME: 4712743a77edSAlan Wright if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC, 4713743a77edSAlan Wright resource1) != 0) { 4714743a77edSAlan Wright (void) no_memory(hdl); 4715743a77edSAlan Wright return (-1); 4716743a77edSAlan Wright } 4717743a77edSAlan Wright if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET, 4718743a77edSAlan Wright resource2) != 0) { 4719743a77edSAlan Wright (void) no_memory(hdl); 4720743a77edSAlan Wright return (-1); 4721743a77edSAlan Wright } 4722743a77edSAlan Wright if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) { 4723743a77edSAlan Wright nvlist_free(nvlist); 4724743a77edSAlan Wright return (-1); 4725743a77edSAlan Wright } 4726743a77edSAlan Wright break; 4727743a77edSAlan Wright case ZFS_SMB_ACL_PURGE: 4728743a77edSAlan Wright break; 4729743a77edSAlan Wright default: 4730743a77edSAlan Wright return (-1); 4731743a77edSAlan Wright } 4732743a77edSAlan Wright error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc); 4733743a77edSAlan Wright nvlist_free(nvlist); 4734743a77edSAlan Wright return (error); 4735743a77edSAlan Wright } 4736743a77edSAlan Wright 4737743a77edSAlan Wright int 4738743a77edSAlan Wright zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset, 4739743a77edSAlan Wright char *path, char *resource) 4740743a77edSAlan Wright { 4741743a77edSAlan Wright return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD, 4742743a77edSAlan Wright resource, NULL)); 4743743a77edSAlan Wright } 4744743a77edSAlan Wright 4745743a77edSAlan Wright int 4746743a77edSAlan Wright zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset, 4747743a77edSAlan Wright char *path, char *resource) 4748743a77edSAlan Wright { 4749743a77edSAlan Wright return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE, 4750743a77edSAlan Wright resource, NULL)); 4751743a77edSAlan Wright } 4752743a77edSAlan Wright 4753743a77edSAlan Wright int 4754743a77edSAlan Wright zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path) 4755743a77edSAlan Wright { 4756743a77edSAlan Wright return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE, 4757743a77edSAlan Wright NULL, NULL)); 4758743a77edSAlan Wright } 4759743a77edSAlan Wright 4760743a77edSAlan Wright int 4761743a77edSAlan Wright zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path, 4762743a77edSAlan Wright char *oldname, char *newname) 4763743a77edSAlan Wright { 4764743a77edSAlan Wright return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME, 4765743a77edSAlan Wright oldname, newname)); 4766743a77edSAlan Wright } 476714843421SMatthew Ahrens 476814843421SMatthew Ahrens int 476914843421SMatthew Ahrens zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type, 477014843421SMatthew Ahrens zfs_userspace_cb_t func, void *arg) 477114843421SMatthew Ahrens { 477214843421SMatthew Ahrens zfs_cmd_t zc = { 0 }; 477314843421SMatthew Ahrens zfs_useracct_t buf[100]; 477470f56fa6SYuri Pankov libzfs_handle_t *hdl = zhp->zfs_hdl; 477570f56fa6SYuri Pankov int ret; 477614843421SMatthew Ahrens 477719b94df9SMatthew Ahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 477814843421SMatthew Ahrens 477914843421SMatthew Ahrens zc.zc_objset_type = type; 478014843421SMatthew Ahrens zc.zc_nvlist_dst = (uintptr_t)buf; 478114843421SMatthew Ahrens 478270f56fa6SYuri Pankov for (;;) { 478314843421SMatthew Ahrens zfs_useracct_t *zua = buf; 478414843421SMatthew Ahrens 478514843421SMatthew Ahrens zc.zc_nvlist_dst_size = sizeof (buf); 478670f56fa6SYuri Pankov if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) { 47873b2aab18SMatthew Ahrens char errbuf[1024]; 478870f56fa6SYuri Pankov 4789f67950b2SNasf-Fan if ((errno == ENOTSUP && 4790f67950b2SNasf-Fan (type == ZFS_PROP_USEROBJUSED || 4791f67950b2SNasf-Fan type == ZFS_PROP_GROUPOBJUSED || 4792f67950b2SNasf-Fan type == ZFS_PROP_USEROBJQUOTA || 4793f67950b2SNasf-Fan type == ZFS_PROP_GROUPOBJQUOTA || 4794f67950b2SNasf-Fan type == ZFS_PROP_PROJECTOBJUSED || 4795f67950b2SNasf-Fan type == ZFS_PROP_PROJECTOBJQUOTA || 4796f67950b2SNasf-Fan type == ZFS_PROP_PROJECTUSED || 4797f67950b2SNasf-Fan type == ZFS_PROP_PROJECTQUOTA))) 4798f67950b2SNasf-Fan break; 4799f67950b2SNasf-Fan 480070f56fa6SYuri Pankov (void) snprintf(errbuf, sizeof (errbuf), 480170f56fa6SYuri Pankov dgettext(TEXT_DOMAIN, 480270f56fa6SYuri Pankov "cannot get used/quota for %s"), zc.zc_name); 480370f56fa6SYuri Pankov return (zfs_standard_error_fmt(hdl, errno, errbuf)); 480470f56fa6SYuri Pankov } 480570f56fa6SYuri Pankov if (zc.zc_nvlist_dst_size == 0) 480614843421SMatthew Ahrens break; 480714843421SMatthew Ahrens 480814843421SMatthew Ahrens while (zc.zc_nvlist_dst_size > 0) { 480970f56fa6SYuri Pankov if ((ret = func(arg, zua->zu_domain, zua->zu_rid, 481070f56fa6SYuri Pankov zua->zu_space)) != 0) 481170f56fa6SYuri Pankov return (ret); 481214843421SMatthew Ahrens zua++; 481314843421SMatthew Ahrens zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t); 481414843421SMatthew Ahrens } 481514843421SMatthew Ahrens } 481614843421SMatthew Ahrens 481770f56fa6SYuri Pankov return (0); 481814843421SMatthew Ahrens } 4819842727c2SChris Kirby 48203b2aab18SMatthew Ahrens struct holdarg { 48213b2aab18SMatthew Ahrens nvlist_t *nvl; 48223b2aab18SMatthew Ahrens const char *snapname; 48233b2aab18SMatthew Ahrens const char *tag; 48243b2aab18SMatthew Ahrens boolean_t recursive; 4825bb6e7075SMatthew Ahrens int error; 48263b2aab18SMatthew Ahrens }; 48273b2aab18SMatthew Ahrens 48283b2aab18SMatthew Ahrens static int 48293b2aab18SMatthew Ahrens zfs_hold_one(zfs_handle_t *zhp, void *arg) 48303b2aab18SMatthew Ahrens { 48313b2aab18SMatthew Ahrens struct holdarg *ha = arg; 48329adfa60dSMatthew Ahrens char name[ZFS_MAX_DATASET_NAME_LEN]; 48333b2aab18SMatthew Ahrens int rv = 0; 48343b2aab18SMatthew Ahrens 48353b2aab18SMatthew Ahrens (void) snprintf(name, sizeof (name), 48363b2aab18SMatthew Ahrens "%s@%s", zhp->zfs_name, ha->snapname); 48373b2aab18SMatthew Ahrens 4838a7a845e4SSteven Hartland if (lzc_exists(name)) 48393b2aab18SMatthew Ahrens fnvlist_add_string(ha->nvl, name, ha->tag); 48403b2aab18SMatthew Ahrens 48413b2aab18SMatthew Ahrens if (ha->recursive) 48423b2aab18SMatthew Ahrens rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha); 48433b2aab18SMatthew Ahrens zfs_close(zhp); 48443b2aab18SMatthew Ahrens return (rv); 48453b2aab18SMatthew Ahrens } 48463b2aab18SMatthew Ahrens 4847842727c2SChris Kirby int 4848842727c2SChris Kirby zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag, 4849a7a845e4SSteven Hartland boolean_t recursive, int cleanup_fd) 4850842727c2SChris Kirby { 48513b2aab18SMatthew Ahrens int ret; 48523b2aab18SMatthew Ahrens struct holdarg ha; 4853842727c2SChris Kirby 48543b2aab18SMatthew Ahrens ha.nvl = fnvlist_alloc(); 48553b2aab18SMatthew Ahrens ha.snapname = snapname; 48563b2aab18SMatthew Ahrens ha.tag = tag; 48573b2aab18SMatthew Ahrens ha.recursive = recursive; 48583b2aab18SMatthew Ahrens (void) zfs_hold_one(zfs_handle_dup(zhp), &ha); 4859013023d4SMartin Matuska 4860a7a845e4SSteven Hartland if (nvlist_empty(ha.nvl)) { 4861a7a845e4SSteven Hartland char errbuf[1024]; 4862a7a845e4SSteven Hartland 4863013023d4SMartin Matuska fnvlist_free(ha.nvl); 4864013023d4SMartin Matuska ret = ENOENT; 4865013023d4SMartin Matuska (void) snprintf(errbuf, sizeof (errbuf), 4866013023d4SMartin Matuska dgettext(TEXT_DOMAIN, 4867013023d4SMartin Matuska "cannot hold snapshot '%s@%s'"), 4868013023d4SMartin Matuska zhp->zfs_name, snapname); 4869a7a845e4SSteven Hartland (void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf); 4870013023d4SMartin Matuska return (ret); 4871013023d4SMartin Matuska } 4872013023d4SMartin Matuska 4873a7a845e4SSteven Hartland ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl); 48743b2aab18SMatthew Ahrens fnvlist_free(ha.nvl); 4875a7f53a56SChris Kirby 4876a7a845e4SSteven Hartland return (ret); 4877a7a845e4SSteven Hartland } 4878842727c2SChris Kirby 4879a7a845e4SSteven Hartland int 4880a7a845e4SSteven Hartland zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds) 4881a7a845e4SSteven Hartland { 4882a7a845e4SSteven Hartland int ret; 4883a7a845e4SSteven Hartland nvlist_t *errors; 4884a7a845e4SSteven Hartland libzfs_handle_t *hdl = zhp->zfs_hdl; 4885a7a845e4SSteven Hartland char errbuf[1024]; 4886a7a845e4SSteven Hartland nvpair_t *elem; 4887a7a845e4SSteven Hartland 4888a7a845e4SSteven Hartland errors = NULL; 4889a7a845e4SSteven Hartland ret = lzc_hold(holds, cleanup_fd, &errors); 4890a7a845e4SSteven Hartland 4891a7a845e4SSteven Hartland if (ret == 0) { 4892a7a845e4SSteven Hartland /* There may be errors even in the success case. */ 4893a7a845e4SSteven Hartland fnvlist_free(errors); 4894a7a845e4SSteven Hartland return (0); 4895a7a845e4SSteven Hartland } 4896a7a845e4SSteven Hartland 4897a7a845e4SSteven Hartland if (nvlist_empty(errors)) { 48983b2aab18SMatthew Ahrens /* no hold-specific errors */ 48993b2aab18SMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), 49003b2aab18SMatthew Ahrens dgettext(TEXT_DOMAIN, "cannot hold")); 49013b2aab18SMatthew Ahrens switch (ret) { 49023b2aab18SMatthew Ahrens case ENOTSUP: 49033b2aab18SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 49043b2aab18SMatthew Ahrens "pool must be upgraded")); 49053b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 49063b2aab18SMatthew Ahrens break; 49073b2aab18SMatthew Ahrens case EINVAL: 49083b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 49093b2aab18SMatthew Ahrens break; 49103b2aab18SMatthew Ahrens default: 49113b2aab18SMatthew Ahrens (void) zfs_standard_error(hdl, ret, errbuf); 49123b2aab18SMatthew Ahrens } 49133b2aab18SMatthew Ahrens } 4914842727c2SChris Kirby 49153b2aab18SMatthew Ahrens for (elem = nvlist_next_nvpair(errors, NULL); 49163b2aab18SMatthew Ahrens elem != NULL; 49173b2aab18SMatthew Ahrens elem = nvlist_next_nvpair(errors, elem)) { 49183b2aab18SMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), 49193b2aab18SMatthew Ahrens dgettext(TEXT_DOMAIN, 49203b2aab18SMatthew Ahrens "cannot hold snapshot '%s'"), nvpair_name(elem)); 49213b2aab18SMatthew Ahrens switch (fnvpair_value_int32(elem)) { 492215508ac0SChris Kirby case E2BIG: 492315508ac0SChris Kirby /* 492415508ac0SChris Kirby * Temporary tags wind up having the ds object id 492515508ac0SChris Kirby * prepended. So even if we passed the length check 492615508ac0SChris Kirby * above, it's still possible for the tag to wind 492715508ac0SChris Kirby * up being slightly too long. 492815508ac0SChris Kirby */ 49293b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf); 49303b2aab18SMatthew Ahrens break; 4931842727c2SChris Kirby case EINVAL: 49323b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 49333b2aab18SMatthew Ahrens break; 4934842727c2SChris Kirby case EEXIST: 49353b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf); 49363b2aab18SMatthew Ahrens break; 4937842727c2SChris Kirby default: 49383b2aab18SMatthew Ahrens (void) zfs_standard_error(hdl, 49393b2aab18SMatthew Ahrens fnvpair_value_int32(elem), errbuf); 4940842727c2SChris Kirby } 4941842727c2SChris Kirby } 4942842727c2SChris Kirby 49433b2aab18SMatthew Ahrens fnvlist_free(errors); 49443b2aab18SMatthew Ahrens return (ret); 49453b2aab18SMatthew Ahrens } 49463b2aab18SMatthew Ahrens 49473b2aab18SMatthew Ahrens static int 49483b2aab18SMatthew Ahrens zfs_release_one(zfs_handle_t *zhp, void *arg) 49493b2aab18SMatthew Ahrens { 49503b2aab18SMatthew Ahrens struct holdarg *ha = arg; 49519adfa60dSMatthew Ahrens char name[ZFS_MAX_DATASET_NAME_LEN]; 49523b2aab18SMatthew Ahrens int rv = 0; 4953bb6e7075SMatthew Ahrens nvlist_t *existing_holds; 49543b2aab18SMatthew Ahrens 49553b2aab18SMatthew Ahrens (void) snprintf(name, sizeof (name), 49563b2aab18SMatthew Ahrens "%s@%s", zhp->zfs_name, ha->snapname); 49573b2aab18SMatthew Ahrens 4958bb6e7075SMatthew Ahrens if (lzc_get_holds(name, &existing_holds) != 0) { 4959bb6e7075SMatthew Ahrens ha->error = ENOENT; 4960bb6e7075SMatthew Ahrens } else if (!nvlist_exists(existing_holds, ha->tag)) { 4961bb6e7075SMatthew Ahrens ha->error = ESRCH; 4962bb6e7075SMatthew Ahrens } else { 4963bb6e7075SMatthew Ahrens nvlist_t *torelease = fnvlist_alloc(); 4964bb6e7075SMatthew Ahrens fnvlist_add_boolean(torelease, ha->tag); 4965bb6e7075SMatthew Ahrens fnvlist_add_nvlist(ha->nvl, name, torelease); 4966bb6e7075SMatthew Ahrens fnvlist_free(torelease); 49673b2aab18SMatthew Ahrens } 49683b2aab18SMatthew Ahrens 49693b2aab18SMatthew Ahrens if (ha->recursive) 49703b2aab18SMatthew Ahrens rv = zfs_iter_filesystems(zhp, zfs_release_one, ha); 49713b2aab18SMatthew Ahrens zfs_close(zhp); 49723b2aab18SMatthew Ahrens return (rv); 4973842727c2SChris Kirby } 4974842727c2SChris Kirby 4975842727c2SChris Kirby int 4976842727c2SChris Kirby zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag, 4977842727c2SChris Kirby boolean_t recursive) 4978842727c2SChris Kirby { 49793b2aab18SMatthew Ahrens int ret; 49803b2aab18SMatthew Ahrens struct holdarg ha; 4981a7a845e4SSteven Hartland nvlist_t *errors = NULL; 49823b2aab18SMatthew Ahrens nvpair_t *elem; 4983842727c2SChris Kirby libzfs_handle_t *hdl = zhp->zfs_hdl; 4984013023d4SMartin Matuska char errbuf[1024]; 4985842727c2SChris Kirby 49863b2aab18SMatthew Ahrens ha.nvl = fnvlist_alloc(); 49873b2aab18SMatthew Ahrens ha.snapname = snapname; 49883b2aab18SMatthew Ahrens ha.tag = tag; 49893b2aab18SMatthew Ahrens ha.recursive = recursive; 4990bb6e7075SMatthew Ahrens ha.error = 0; 49913b2aab18SMatthew Ahrens (void) zfs_release_one(zfs_handle_dup(zhp), &ha); 4992013023d4SMartin Matuska 4993a7a845e4SSteven Hartland if (nvlist_empty(ha.nvl)) { 4994013023d4SMartin Matuska fnvlist_free(ha.nvl); 4995bb6e7075SMatthew Ahrens ret = ha.error; 4996013023d4SMartin Matuska (void) snprintf(errbuf, sizeof (errbuf), 4997013023d4SMartin Matuska dgettext(TEXT_DOMAIN, 4998013023d4SMartin Matuska "cannot release hold from snapshot '%s@%s'"), 4999013023d4SMartin Matuska zhp->zfs_name, snapname); 5000bb6e7075SMatthew Ahrens if (ret == ESRCH) { 5001bb6e7075SMatthew Ahrens (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf); 5002bb6e7075SMatthew Ahrens } else { 5003013023d4SMartin Matuska (void) zfs_standard_error(hdl, ret, errbuf); 5004bb6e7075SMatthew Ahrens } 5005013023d4SMartin Matuska return (ret); 5006013023d4SMartin Matuska } 5007013023d4SMartin Matuska 50083b2aab18SMatthew Ahrens ret = lzc_release(ha.nvl, &errors); 50093b2aab18SMatthew Ahrens fnvlist_free(ha.nvl); 5010842727c2SChris Kirby 5011a7a845e4SSteven Hartland if (ret == 0) { 5012a7a845e4SSteven Hartland /* There may be errors even in the success case. */ 5013a7a845e4SSteven Hartland fnvlist_free(errors); 50143b2aab18SMatthew Ahrens return (0); 5015a7a845e4SSteven Hartland } 5016842727c2SChris Kirby 5017a7a845e4SSteven Hartland if (nvlist_empty(errors)) { 50183b2aab18SMatthew Ahrens /* no hold-specific errors */ 5019842727c2SChris Kirby (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 50203b2aab18SMatthew Ahrens "cannot release")); 5021842727c2SChris Kirby switch (errno) { 5022842727c2SChris Kirby case ENOTSUP: 5023842727c2SChris Kirby zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5024842727c2SChris Kirby "pool must be upgraded")); 50253b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 50263b2aab18SMatthew Ahrens break; 5027842727c2SChris Kirby default: 50283b2aab18SMatthew Ahrens (void) zfs_standard_error_fmt(hdl, errno, errbuf); 5029842727c2SChris Kirby } 5030842727c2SChris Kirby } 5031842727c2SChris Kirby 50323b2aab18SMatthew Ahrens for (elem = nvlist_next_nvpair(errors, NULL); 50333b2aab18SMatthew Ahrens elem != NULL; 50343b2aab18SMatthew Ahrens elem = nvlist_next_nvpair(errors, elem)) { 50353b2aab18SMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), 50363b2aab18SMatthew Ahrens dgettext(TEXT_DOMAIN, 50373b2aab18SMatthew Ahrens "cannot release hold from snapshot '%s'"), 50383b2aab18SMatthew Ahrens nvpair_name(elem)); 50393b2aab18SMatthew Ahrens switch (fnvpair_value_int32(elem)) { 50403b2aab18SMatthew Ahrens case ESRCH: 50413b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf); 50423b2aab18SMatthew Ahrens break; 50433b2aab18SMatthew Ahrens case EINVAL: 50443b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 50453b2aab18SMatthew Ahrens break; 50463b2aab18SMatthew Ahrens default: 50473b2aab18SMatthew Ahrens (void) zfs_standard_error_fmt(hdl, 50483b2aab18SMatthew Ahrens fnvpair_value_int32(elem), errbuf); 50493b2aab18SMatthew Ahrens } 50503b2aab18SMatthew Ahrens } 50513b2aab18SMatthew Ahrens 50523b2aab18SMatthew Ahrens fnvlist_free(errors); 50533b2aab18SMatthew Ahrens return (ret); 5054842727c2SChris Kirby } 5055ca45db41SChris Kirby 50561af68beaSAlexander Stetsenko int 50571af68beaSAlexander Stetsenko zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl) 50581af68beaSAlexander Stetsenko { 50591af68beaSAlexander Stetsenko zfs_cmd_t zc = { 0 }; 50601af68beaSAlexander Stetsenko libzfs_handle_t *hdl = zhp->zfs_hdl; 50611af68beaSAlexander Stetsenko int nvsz = 2048; 50621af68beaSAlexander Stetsenko void *nvbuf; 50631af68beaSAlexander Stetsenko int err = 0; 50643b2aab18SMatthew Ahrens char errbuf[1024]; 50651af68beaSAlexander Stetsenko 50661af68beaSAlexander Stetsenko assert(zhp->zfs_type == ZFS_TYPE_VOLUME || 50671af68beaSAlexander Stetsenko zhp->zfs_type == ZFS_TYPE_FILESYSTEM); 50681af68beaSAlexander Stetsenko 50691af68beaSAlexander Stetsenko tryagain: 50701af68beaSAlexander Stetsenko 50711af68beaSAlexander Stetsenko nvbuf = malloc(nvsz); 50721af68beaSAlexander Stetsenko if (nvbuf == NULL) { 50731af68beaSAlexander Stetsenko err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno))); 50741af68beaSAlexander Stetsenko goto out; 50751af68beaSAlexander Stetsenko } 50761af68beaSAlexander Stetsenko 50771af68beaSAlexander Stetsenko zc.zc_nvlist_dst_size = nvsz; 50781af68beaSAlexander Stetsenko zc.zc_nvlist_dst = (uintptr_t)nvbuf; 50791af68beaSAlexander Stetsenko 50809adfa60dSMatthew Ahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 50811af68beaSAlexander Stetsenko 5082d7f601efSGeorge Wilson if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) { 50831af68beaSAlexander Stetsenko (void) snprintf(errbuf, sizeof (errbuf), 50841af68beaSAlexander Stetsenko dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"), 50851af68beaSAlexander Stetsenko zc.zc_name); 50861af68beaSAlexander Stetsenko switch (errno) { 50871af68beaSAlexander Stetsenko case ENOMEM: 50881af68beaSAlexander Stetsenko free(nvbuf); 50891af68beaSAlexander Stetsenko nvsz = zc.zc_nvlist_dst_size; 50901af68beaSAlexander Stetsenko goto tryagain; 50911af68beaSAlexander Stetsenko 50921af68beaSAlexander Stetsenko case ENOTSUP: 50931af68beaSAlexander Stetsenko zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 50941af68beaSAlexander Stetsenko "pool must be upgraded")); 50951af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADVERSION, errbuf); 50961af68beaSAlexander Stetsenko break; 50971af68beaSAlexander Stetsenko case EINVAL: 50981af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADTYPE, errbuf); 50991af68beaSAlexander Stetsenko break; 51001af68beaSAlexander Stetsenko case ENOENT: 51011af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_NOENT, errbuf); 51021af68beaSAlexander Stetsenko break; 51031af68beaSAlexander Stetsenko default: 51041af68beaSAlexander Stetsenko err = zfs_standard_error_fmt(hdl, errno, errbuf); 51051af68beaSAlexander Stetsenko break; 51061af68beaSAlexander Stetsenko } 51071af68beaSAlexander Stetsenko } else { 51081af68beaSAlexander Stetsenko /* success */ 51091af68beaSAlexander Stetsenko int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0); 51101af68beaSAlexander Stetsenko if (rc) { 51111af68beaSAlexander Stetsenko (void) snprintf(errbuf, sizeof (errbuf), dgettext( 51121af68beaSAlexander Stetsenko TEXT_DOMAIN, "cannot get permissions on '%s'"), 51131af68beaSAlexander Stetsenko zc.zc_name); 51141af68beaSAlexander Stetsenko err = zfs_standard_error_fmt(hdl, rc, errbuf); 51151af68beaSAlexander Stetsenko } 51161af68beaSAlexander Stetsenko } 51171af68beaSAlexander Stetsenko 51181af68beaSAlexander Stetsenko free(nvbuf); 51191af68beaSAlexander Stetsenko out: 51201af68beaSAlexander Stetsenko return (err); 51211af68beaSAlexander Stetsenko } 51221af68beaSAlexander Stetsenko 51231af68beaSAlexander Stetsenko int 51241af68beaSAlexander Stetsenko zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl) 51251af68beaSAlexander Stetsenko { 51261af68beaSAlexander Stetsenko zfs_cmd_t zc = { 0 }; 51271af68beaSAlexander Stetsenko libzfs_handle_t *hdl = zhp->zfs_hdl; 51281af68beaSAlexander Stetsenko char *nvbuf; 51293b2aab18SMatthew Ahrens char errbuf[1024]; 51301af68beaSAlexander Stetsenko size_t nvsz; 51311af68beaSAlexander Stetsenko int err; 51321af68beaSAlexander Stetsenko 51331af68beaSAlexander Stetsenko assert(zhp->zfs_type == ZFS_TYPE_VOLUME || 51341af68beaSAlexander Stetsenko zhp->zfs_type == ZFS_TYPE_FILESYSTEM); 51351af68beaSAlexander Stetsenko 51361af68beaSAlexander Stetsenko err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE); 51371af68beaSAlexander Stetsenko assert(err == 0); 51381af68beaSAlexander Stetsenko 51391af68beaSAlexander Stetsenko nvbuf = malloc(nvsz); 51401af68beaSAlexander Stetsenko 51411af68beaSAlexander Stetsenko err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0); 51421af68beaSAlexander Stetsenko assert(err == 0); 51431af68beaSAlexander Stetsenko 51441af68beaSAlexander Stetsenko zc.zc_nvlist_src_size = nvsz; 51451af68beaSAlexander Stetsenko zc.zc_nvlist_src = (uintptr_t)nvbuf; 51461af68beaSAlexander Stetsenko zc.zc_perm_action = un; 51471af68beaSAlexander Stetsenko 51481af68beaSAlexander Stetsenko (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 51491af68beaSAlexander Stetsenko 51501af68beaSAlexander Stetsenko if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) { 51511af68beaSAlexander Stetsenko (void) snprintf(errbuf, sizeof (errbuf), 51521af68beaSAlexander Stetsenko dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"), 51531af68beaSAlexander Stetsenko zc.zc_name); 51541af68beaSAlexander Stetsenko switch (errno) { 51551af68beaSAlexander Stetsenko case ENOTSUP: 51561af68beaSAlexander Stetsenko zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 51571af68beaSAlexander Stetsenko "pool must be upgraded")); 51581af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADVERSION, errbuf); 51591af68beaSAlexander Stetsenko break; 51601af68beaSAlexander Stetsenko case EINVAL: 51611af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADTYPE, errbuf); 51621af68beaSAlexander Stetsenko break; 51631af68beaSAlexander Stetsenko case ENOENT: 51641af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_NOENT, errbuf); 51651af68beaSAlexander Stetsenko break; 51661af68beaSAlexander Stetsenko default: 51671af68beaSAlexander Stetsenko err = zfs_standard_error_fmt(hdl, errno, errbuf); 51681af68beaSAlexander Stetsenko break; 51691af68beaSAlexander Stetsenko } 51701af68beaSAlexander Stetsenko } 51711af68beaSAlexander Stetsenko 51721af68beaSAlexander Stetsenko free(nvbuf); 51731af68beaSAlexander Stetsenko 51741af68beaSAlexander Stetsenko return (err); 51751af68beaSAlexander Stetsenko } 51761af68beaSAlexander Stetsenko 51771af68beaSAlexander Stetsenko int 51781af68beaSAlexander Stetsenko zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl) 51791af68beaSAlexander Stetsenko { 51803b2aab18SMatthew Ahrens int err; 51813b2aab18SMatthew Ahrens char errbuf[1024]; 51823b2aab18SMatthew Ahrens 51833b2aab18SMatthew Ahrens err = lzc_get_holds(zhp->zfs_name, nvl); 51843b2aab18SMatthew Ahrens 51853b2aab18SMatthew Ahrens if (err != 0) { 51861af68beaSAlexander Stetsenko libzfs_handle_t *hdl = zhp->zfs_hdl; 51871af68beaSAlexander Stetsenko 51881af68beaSAlexander Stetsenko (void) snprintf(errbuf, sizeof (errbuf), 51891af68beaSAlexander Stetsenko dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"), 51903b2aab18SMatthew Ahrens zhp->zfs_name); 51913b2aab18SMatthew Ahrens switch (err) { 51921af68beaSAlexander Stetsenko case ENOTSUP: 51931af68beaSAlexander Stetsenko zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 51941af68beaSAlexander Stetsenko "pool must be upgraded")); 51951af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADVERSION, errbuf); 51961af68beaSAlexander Stetsenko break; 51971af68beaSAlexander Stetsenko case EINVAL: 51981af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADTYPE, errbuf); 51991af68beaSAlexander Stetsenko break; 52001af68beaSAlexander Stetsenko case ENOENT: 52011af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_NOENT, errbuf); 52021af68beaSAlexander Stetsenko break; 52031af68beaSAlexander Stetsenko default: 52041af68beaSAlexander Stetsenko err = zfs_standard_error_fmt(hdl, errno, errbuf); 52051af68beaSAlexander Stetsenko break; 52061af68beaSAlexander Stetsenko } 52071af68beaSAlexander Stetsenko } 52081af68beaSAlexander Stetsenko 52091af68beaSAlexander Stetsenko return (err); 52101af68beaSAlexander Stetsenko } 52111af68beaSAlexander Stetsenko 52123e30c24aSWill Andrews /* 5213b73ccab0SMike Gerdts * The theory of raidz space accounting 5214b73ccab0SMike Gerdts * 5215b73ccab0SMike Gerdts * The "referenced" property of RAIDZ vdevs is scaled such that a 128KB block 5216b73ccab0SMike Gerdts * will "reference" 128KB, even though it allocates more than that, to store the 5217b73ccab0SMike Gerdts * parity information (and perhaps skip sectors). This concept of the 5218b73ccab0SMike Gerdts * "referenced" (and other DMU space accounting) being lower than the allocated 5219b73ccab0SMike Gerdts * space by a constant factor is called "raidz deflation." 5220b73ccab0SMike Gerdts * 5221b73ccab0SMike Gerdts * As mentioned above, the constant factor for raidz deflation assumes a 128KB 5222b73ccab0SMike Gerdts * block size. However, zvols typically have a much smaller block size (default 5223b73ccab0SMike Gerdts * 8KB). These smaller blocks may require proportionally much more parity 5224b73ccab0SMike Gerdts * information (and perhaps skip sectors). In this case, the change to the 5225b73ccab0SMike Gerdts * "referenced" property may be much more than the logical block size. 5226b73ccab0SMike Gerdts * 5227b73ccab0SMike Gerdts * Suppose a raidz vdev has 5 disks with ashift=12. A 128k block may be written 5228b73ccab0SMike Gerdts * as follows. 5229b73ccab0SMike Gerdts * 5230b73ccab0SMike Gerdts * +-------+-------+-------+-------+-------+ 5231b73ccab0SMike Gerdts * | disk1 | disk2 | disk3 | disk4 | disk5 | 5232b73ccab0SMike Gerdts * +-------+-------+-------+-------+-------+ 5233b73ccab0SMike Gerdts * | P0 | D0 | D8 | D16 | D24 | 5234b73ccab0SMike Gerdts * | P1 | D1 | D9 | D17 | D25 | 5235b73ccab0SMike Gerdts * | P2 | D2 | D10 | D18 | D26 | 5236b73ccab0SMike Gerdts * | P3 | D3 | D11 | D19 | D27 | 5237b73ccab0SMike Gerdts * | P4 | D4 | D12 | D20 | D28 | 5238b73ccab0SMike Gerdts * | P5 | D5 | D13 | D21 | D29 | 5239b73ccab0SMike Gerdts * | P6 | D6 | D14 | D22 | D30 | 5240b73ccab0SMike Gerdts * | P7 | D7 | D15 | D23 | D31 | 5241b73ccab0SMike Gerdts * +-------+-------+-------+-------+-------+ 5242b73ccab0SMike Gerdts * 5243b73ccab0SMike Gerdts * Above, notice that 160k was allocated: 8 x 4k parity sectors + 32 x 4k data 5244b73ccab0SMike Gerdts * sectors. The dataset's referenced will increase by 128k and the pool's 5245b73ccab0SMike Gerdts * allocated and free properties will be adjusted by 160k. 5246b73ccab0SMike Gerdts * 5247b73ccab0SMike Gerdts * A 4k block written to the same raidz vdev will require two 4k sectors. The 5248b73ccab0SMike Gerdts * blank cells represent unallocated space. 5249b73ccab0SMike Gerdts * 5250b73ccab0SMike Gerdts * +-------+-------+-------+-------+-------+ 5251b73ccab0SMike Gerdts * | disk1 | disk2 | disk3 | disk4 | disk5 | 5252b73ccab0SMike Gerdts * +-------+-------+-------+-------+-------+ 5253b73ccab0SMike Gerdts * | P0 | D0 | | | | 5254b73ccab0SMike Gerdts * +-------+-------+-------+-------+-------+ 5255b73ccab0SMike Gerdts * 5256b73ccab0SMike Gerdts * Above, notice that the 4k block required one sector for parity and another 5257b73ccab0SMike Gerdts * for data. vdev_raidz_asize() will return 8k and as such the pool's allocated 5258b73ccab0SMike Gerdts * and free properties will be adjusted by 8k. The dataset will not be charged 5259b73ccab0SMike Gerdts * 8k. Rather, it will be charged a value that is scaled according to the 5260b73ccab0SMike Gerdts * overhead of the 128k block on the same vdev. This 8k allocation will be 5261b73ccab0SMike Gerdts * charged 8k * 128k / 160k. 128k is from SPA_OLD_MAXBLOCKSIZE and 160k is as 5262b73ccab0SMike Gerdts * calculated in the 128k block example above. 5263b73ccab0SMike Gerdts * 5264b73ccab0SMike Gerdts * Every raidz allocation is sized to be a multiple of nparity+1 sectors. That 5265b73ccab0SMike Gerdts * is, every raidz1 allocation will be a multiple of 2 sectors, raidz2 5266b73ccab0SMike Gerdts * allocations are a multiple of 3 sectors, and raidz3 allocations are a 5267b73ccab0SMike Gerdts * multiple of of 4 sectors. When a block does not fill the required number of 5268b73ccab0SMike Gerdts * sectors, skip blocks (sectors) are used. 5269b73ccab0SMike Gerdts * 5270b73ccab0SMike Gerdts * An 8k block being written to a raidz vdev may be written as follows: 5271b73ccab0SMike Gerdts * 5272b73ccab0SMike Gerdts * +-------+-------+-------+-------+-------+ 5273b73ccab0SMike Gerdts * | disk1 | disk2 | disk3 | disk4 | disk5 | 5274b73ccab0SMike Gerdts * +-------+-------+-------+-------+-------+ 5275b73ccab0SMike Gerdts * | P0 | D0 | D1 | S0 | | 5276b73ccab0SMike Gerdts * +-------+-------+-------+-------+-------+ 5277b73ccab0SMike Gerdts * 5278b73ccab0SMike Gerdts * In order to maintain the nparity+1 allocation size, a skip block (S0) was 5279b73ccab0SMike Gerdts * added. For this 8k block, the pool's allocated and free properties are 5280b73ccab0SMike Gerdts * adjusted by 16k and the dataset's referenced is increased by 16k * 128k / 5281b73ccab0SMike Gerdts * 160k. Again, 128k is from SPA_OLD_MAXBLOCKSIZE and 160k is as calculated in 5282b73ccab0SMike Gerdts * the 128k block example above. 5283b73ccab0SMike Gerdts * 5284b73ccab0SMike Gerdts * Compression may lead to a variety of block sizes being written for the same 5285b73ccab0SMike Gerdts * volume or file. There is no clear way to reserve just the amount of space 5286b73ccab0SMike Gerdts * that will be required, so the worst case (no compression) is assumed. 5287b73ccab0SMike Gerdts * Note that metadata blocks will typically be compressed, so the reservation 5288b73ccab0SMike Gerdts * size returned by zvol_volsize_to_reservation() will generally be slightly 5289b73ccab0SMike Gerdts * larger than the maximum that the volume can reference. 5290b73ccab0SMike Gerdts */ 5291b73ccab0SMike Gerdts 5292b73ccab0SMike Gerdts /* 5293b73ccab0SMike Gerdts * Derived from function of same name in uts/common/fs/zfs/vdev_raidz.c. 5294b73ccab0SMike Gerdts * Returns the amount of space (in bytes) that will be allocated for the 5295b73ccab0SMike Gerdts * specified block size. Note that the "referenced" space accounted will be less 5296b73ccab0SMike Gerdts * than this, but not necessarily equal to "blksize", due to RAIDZ deflation. 5297b73ccab0SMike Gerdts */ 5298b73ccab0SMike Gerdts static uint64_t 5299b73ccab0SMike Gerdts vdev_raidz_asize(uint64_t ndisks, uint64_t nparity, uint64_t ashift, 5300b73ccab0SMike Gerdts uint64_t blksize) 5301b73ccab0SMike Gerdts { 5302b73ccab0SMike Gerdts uint64_t asize, ndata; 5303b73ccab0SMike Gerdts 5304b73ccab0SMike Gerdts ASSERT3U(ndisks, >, nparity); 5305b73ccab0SMike Gerdts ndata = ndisks - nparity; 5306b73ccab0SMike Gerdts asize = ((blksize - 1) >> ashift) + 1; 5307b73ccab0SMike Gerdts asize += nparity * ((asize + ndata - 1) / ndata); 5308b73ccab0SMike Gerdts asize = roundup(asize, nparity + 1) << ashift; 5309b73ccab0SMike Gerdts 5310b73ccab0SMike Gerdts return (asize); 5311b73ccab0SMike Gerdts } 5312b73ccab0SMike Gerdts 5313b73ccab0SMike Gerdts /* 5314b73ccab0SMike Gerdts * Determine how much space will be allocated if it lands on the most space- 5315b73ccab0SMike Gerdts * inefficient top-level vdev. Returns the size in bytes required to store one 5316b73ccab0SMike Gerdts * copy of the volume data. See theory comment above. 5317b73ccab0SMike Gerdts */ 5318b73ccab0SMike Gerdts static uint64_t 5319b73ccab0SMike Gerdts volsize_from_vdevs(zpool_handle_t *zhp, uint64_t nblocks, uint64_t blksize) 5320b73ccab0SMike Gerdts { 5321b73ccab0SMike Gerdts nvlist_t *config, *tree, **vdevs; 5322b73ccab0SMike Gerdts uint_t nvdevs, v; 5323b73ccab0SMike Gerdts uint64_t ret = 0; 5324b73ccab0SMike Gerdts 5325b73ccab0SMike Gerdts config = zpool_get_config(zhp, NULL); 5326b73ccab0SMike Gerdts if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree) != 0 || 5327b73ccab0SMike Gerdts nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, 5328b73ccab0SMike Gerdts &vdevs, &nvdevs) != 0) { 5329b73ccab0SMike Gerdts return (nblocks * blksize); 5330b73ccab0SMike Gerdts } 5331b73ccab0SMike Gerdts 5332b73ccab0SMike Gerdts for (v = 0; v < nvdevs; v++) { 5333b73ccab0SMike Gerdts char *type; 5334b73ccab0SMike Gerdts uint64_t nparity, ashift, asize, tsize; 5335b73ccab0SMike Gerdts nvlist_t **disks; 5336b73ccab0SMike Gerdts uint_t ndisks; 5337b73ccab0SMike Gerdts uint64_t volsize; 5338b73ccab0SMike Gerdts 5339b73ccab0SMike Gerdts if (nvlist_lookup_string(vdevs[v], ZPOOL_CONFIG_TYPE, 5340b73ccab0SMike Gerdts &type) != 0 || strcmp(type, VDEV_TYPE_RAIDZ) != 0 || 5341b73ccab0SMike Gerdts nvlist_lookup_uint64(vdevs[v], ZPOOL_CONFIG_NPARITY, 5342b73ccab0SMike Gerdts &nparity) != 0 || 5343b73ccab0SMike Gerdts nvlist_lookup_uint64(vdevs[v], ZPOOL_CONFIG_ASHIFT, 5344b73ccab0SMike Gerdts &ashift) != 0 || 5345b73ccab0SMike Gerdts nvlist_lookup_nvlist_array(vdevs[v], ZPOOL_CONFIG_CHILDREN, 5346b73ccab0SMike Gerdts &disks, &ndisks) != 0) { 5347b73ccab0SMike Gerdts continue; 5348b73ccab0SMike Gerdts } 5349b73ccab0SMike Gerdts 5350b73ccab0SMike Gerdts /* allocation size for the "typical" 128k block */ 5351b73ccab0SMike Gerdts tsize = vdev_raidz_asize(ndisks, nparity, ashift, 5352b73ccab0SMike Gerdts SPA_OLD_MAXBLOCKSIZE); 5353b73ccab0SMike Gerdts /* allocation size for the blksize block */ 5354b73ccab0SMike Gerdts asize = vdev_raidz_asize(ndisks, nparity, ashift, blksize); 5355b73ccab0SMike Gerdts 5356b73ccab0SMike Gerdts /* 5357b73ccab0SMike Gerdts * Scale this size down as a ratio of 128k / tsize. See theory 5358b73ccab0SMike Gerdts * statement above. 5359b73ccab0SMike Gerdts */ 5360b73ccab0SMike Gerdts volsize = nblocks * asize * SPA_OLD_MAXBLOCKSIZE / tsize; 5361b73ccab0SMike Gerdts if (volsize > ret) { 5362b73ccab0SMike Gerdts ret = volsize; 5363b73ccab0SMike Gerdts } 5364b73ccab0SMike Gerdts } 5365b73ccab0SMike Gerdts 5366b73ccab0SMike Gerdts if (ret == 0) { 5367b73ccab0SMike Gerdts ret = nblocks * blksize; 5368b73ccab0SMike Gerdts } 5369b73ccab0SMike Gerdts 5370b73ccab0SMike Gerdts return (ret); 5371b73ccab0SMike Gerdts } 5372b73ccab0SMike Gerdts 5373b73ccab0SMike Gerdts /* 5374b73ccab0SMike Gerdts * Convert the zvol's volume size to an appropriate reservation. See theory 5375b73ccab0SMike Gerdts * comment above. 5376b73ccab0SMike Gerdts * 53773e30c24aSWill Andrews * Note: If this routine is updated, it is necessary to update the ZFS test 5378b73ccab0SMike Gerdts * suite's shell version in reservation.shlib. 53793e30c24aSWill Andrews */ 5380c1449561SEric Taylor uint64_t 5381b73ccab0SMike Gerdts zvol_volsize_to_reservation(zpool_handle_t *zph, uint64_t volsize, 5382b73ccab0SMike Gerdts nvlist_t *props) 5383c1449561SEric Taylor { 5384c1449561SEric Taylor uint64_t numdb; 5385c1449561SEric Taylor uint64_t nblocks, volblocksize; 5386c1449561SEric Taylor int ncopies; 5387c1449561SEric Taylor char *strval; 5388c1449561SEric Taylor 5389c1449561SEric Taylor if (nvlist_lookup_string(props, 5390c1449561SEric Taylor zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0) 5391c1449561SEric Taylor ncopies = atoi(strval); 5392c1449561SEric Taylor else 5393c1449561SEric Taylor ncopies = 1; 5394c1449561SEric Taylor if (nvlist_lookup_uint64(props, 5395c1449561SEric Taylor zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 5396c1449561SEric Taylor &volblocksize) != 0) 5397c1449561SEric Taylor volblocksize = ZVOL_DEFAULT_BLOCKSIZE; 5398b73ccab0SMike Gerdts 5399c1449561SEric Taylor nblocks = volsize / volblocksize; 5400b73ccab0SMike Gerdts /* 5401b73ccab0SMike Gerdts * Metadata defaults to using 128k blocks, not volblocksize blocks. For 5402b73ccab0SMike Gerdts * this reason, only the data blocks are scaled based on vdev config. 5403b73ccab0SMike Gerdts */ 5404b73ccab0SMike Gerdts volsize = volsize_from_vdevs(zph, nblocks, volblocksize); 5405b73ccab0SMike Gerdts 5406c1449561SEric Taylor /* start with metadnode L0-L6 */ 5407c1449561SEric Taylor numdb = 7; 5408c1449561SEric Taylor /* calculate number of indirects */ 5409c1449561SEric Taylor while (nblocks > 1) { 5410c1449561SEric Taylor nblocks += DNODES_PER_LEVEL - 1; 5411c1449561SEric Taylor nblocks /= DNODES_PER_LEVEL; 5412c1449561SEric Taylor numdb += nblocks; 5413c1449561SEric Taylor } 5414c1449561SEric Taylor numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1); 5415c1449561SEric Taylor volsize *= ncopies; 5416c1449561SEric Taylor /* 5417c1449561SEric Taylor * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't 5418c1449561SEric Taylor * compressed, but in practice they compress down to about 5419c1449561SEric Taylor * 1100 bytes 5420c1449561SEric Taylor */ 5421c1449561SEric Taylor numdb *= 1ULL << DN_MAX_INDBLKSHIFT; 5422c1449561SEric Taylor volsize += numdb; 5423c1449561SEric Taylor return (volsize); 5424c1449561SEric Taylor } 5425