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. 241c10ae76SMike Gerdts * Copyright (c) 2018, Joyent, Inc. All rights reserved. 25ad2760acSMatthew Ahrens * Copyright (c) 2011, 2016 by Delphix. All rights reserved. 26f0f3ef5aSGarrett D'Amore * Copyright (c) 2012 DEY Storage Systems, Inc. All rights reserved. 270d8fa8f8SMartin Matuska * Copyright (c) 2011-2012 Pawel Jakub Dawidek. All rights reserved. 28013023d4SMartin Matuska * Copyright (c) 2013 Martin Matuska. All rights reserved. 29a7a845e4SSteven Hartland * Copyright (c) 2013 Steven Hartland. All rights reserved. 30c3d26abcSMatthew Ahrens * Copyright (c) 2014 Integros [integros.com] 319fa2266dSYuri Pankov * Copyright 2017 Nexenta Systems, Inc. 3288f61deeSIgor Kozhukhov * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com> 33f62db44dSAndrew Stormont * Copyright 2017-2018 RackTop Systems. 34fa9e4066Sahrens */ 35fa9e4066Sahrens 36fa9e4066Sahrens #include <ctype.h> 37fa9e4066Sahrens #include <errno.h> 38fa9e4066Sahrens #include <libintl.h> 39fa9e4066Sahrens #include <math.h> 40fa9e4066Sahrens #include <stdio.h> 41fa9e4066Sahrens #include <stdlib.h> 42fa9e4066Sahrens #include <strings.h> 43fa9e4066Sahrens #include <unistd.h> 443cb34c60Sahrens #include <stddef.h> 45fa9e4066Sahrens #include <zone.h> 4699653d4eSeschrock #include <fcntl.h> 47fa9e4066Sahrens #include <sys/mntent.h> 48b12a1c38Slling #include <sys/mount.h> 49ecd6cf80Smarks #include <priv.h> 50ecd6cf80Smarks #include <pwd.h> 51ecd6cf80Smarks #include <grp.h> 52ecd6cf80Smarks #include <stddef.h> 53ecd6cf80Smarks #include <ucred.h> 5414843421SMatthew Ahrens #include <idmap.h> 5514843421SMatthew Ahrens #include <aclutils.h> 563b12c289SMatthew Ahrens #include <directory.h> 57591e0e13SSebastien Roy #include <time.h> 58fa9e4066Sahrens 59c1449561SEric Taylor #include <sys/dnode.h> 60fa9e4066Sahrens #include <sys/spa.h> 61e9dbad6fSeschrock #include <sys/zap.h> 62fa9e4066Sahrens #include <libzfs.h> 63fa9e4066Sahrens 64fa9e4066Sahrens #include "zfs_namecheck.h" 65fa9e4066Sahrens #include "zfs_prop.h" 66fa9e4066Sahrens #include "libzfs_impl.h" 67ecd6cf80Smarks #include "zfs_deleg.h" 68fa9e4066Sahrens 6914843421SMatthew Ahrens static int userquota_propname_decode(const char *propname, boolean_t zoned, 7014843421SMatthew Ahrens zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp); 71cdf5b4caSmmusante 72fa9e4066Sahrens /* 73fa9e4066Sahrens * Given a single type (not a mask of types), return the type in a human 74fa9e4066Sahrens * readable form. 75fa9e4066Sahrens */ 76fa9e4066Sahrens const char * 77fa9e4066Sahrens zfs_type_to_name(zfs_type_t type) 78fa9e4066Sahrens { 79fa9e4066Sahrens switch (type) { 80fa9e4066Sahrens case ZFS_TYPE_FILESYSTEM: 81fa9e4066Sahrens return (dgettext(TEXT_DOMAIN, "filesystem")); 82fa9e4066Sahrens case ZFS_TYPE_SNAPSHOT: 83fa9e4066Sahrens return (dgettext(TEXT_DOMAIN, "snapshot")); 84fa9e4066Sahrens case ZFS_TYPE_VOLUME: 85fa9e4066Sahrens return (dgettext(TEXT_DOMAIN, "volume")); 8688f61deeSIgor Kozhukhov case ZFS_TYPE_POOL: 8788f61deeSIgor Kozhukhov return (dgettext(TEXT_DOMAIN, "pool")); 8888f61deeSIgor Kozhukhov case ZFS_TYPE_BOOKMARK: 8988f61deeSIgor Kozhukhov return (dgettext(TEXT_DOMAIN, "bookmark")); 9088f61deeSIgor Kozhukhov default: 9188f61deeSIgor Kozhukhov assert(!"unhandled zfs_type_t"); 92fa9e4066Sahrens } 93fa9e4066Sahrens 94fa9e4066Sahrens return (NULL); 95fa9e4066Sahrens } 96fa9e4066Sahrens 97fa9e4066Sahrens /* 98fa9e4066Sahrens * Validate a ZFS path. This is used even before trying to open the dataset, to 9914843421SMatthew Ahrens * provide a more meaningful error message. We call zfs_error_aux() to 10014843421SMatthew Ahrens * explain exactly why the name was not valid. 101fa9e4066Sahrens */ 10299d5e173STim Haley int 103f18faf3fSek zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type, 104f18faf3fSek boolean_t modifying) 105fa9e4066Sahrens { 106fa9e4066Sahrens namecheck_err_t why; 107fa9e4066Sahrens char what; 108fa9e4066Sahrens 109edb901aaSMarcel Telka if (entity_namecheck(path, &why, &what) != 0) { 11099653d4eSeschrock if (hdl != NULL) { 111fa9e4066Sahrens switch (why) { 112b81d61a6Slling case NAME_ERR_TOOLONG: 11399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11499653d4eSeschrock "name is too long")); 115b81d61a6Slling break; 116b81d61a6Slling 117fa9e4066Sahrens case NAME_ERR_LEADING_SLASH: 11899653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11999653d4eSeschrock "leading slash in name")); 120fa9e4066Sahrens break; 121fa9e4066Sahrens 122fa9e4066Sahrens case NAME_ERR_EMPTY_COMPONENT: 12399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 12499653d4eSeschrock "empty component in name")); 125fa9e4066Sahrens break; 126fa9e4066Sahrens 127fa9e4066Sahrens case NAME_ERR_TRAILING_SLASH: 12899653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 12999653d4eSeschrock "trailing slash in name")); 130fa9e4066Sahrens break; 131fa9e4066Sahrens 132fa9e4066Sahrens case NAME_ERR_INVALCHAR: 13399653d4eSeschrock zfs_error_aux(hdl, 134fa9e4066Sahrens dgettext(TEXT_DOMAIN, "invalid character " 13599653d4eSeschrock "'%c' in name"), what); 136fa9e4066Sahrens break; 137fa9e4066Sahrens 138edb901aaSMarcel Telka case NAME_ERR_MULTIPLE_DELIMITERS: 13999653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 140edb901aaSMarcel Telka "multiple '@' and/or '#' delimiters in " 141edb901aaSMarcel Telka "name")); 142fa9e4066Sahrens break; 1435ad82045Snd 1445ad82045Snd case NAME_ERR_NOLETTER: 1455ad82045Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1465ad82045Snd "pool doesn't begin with a letter")); 1475ad82045Snd break; 1485ad82045Snd 1495ad82045Snd case NAME_ERR_RESERVED: 1505ad82045Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1515ad82045Snd "name is reserved")); 1525ad82045Snd break; 1535ad82045Snd 1545ad82045Snd case NAME_ERR_DISKLIKE: 1555ad82045Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1565ad82045Snd "reserved disk name")); 1575ad82045Snd break; 15888f61deeSIgor Kozhukhov 15988f61deeSIgor Kozhukhov default: 16088f61deeSIgor Kozhukhov zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 16188f61deeSIgor Kozhukhov "(%d) not defined"), why); 16288f61deeSIgor Kozhukhov break; 163fa9e4066Sahrens } 164fa9e4066Sahrens } 165fa9e4066Sahrens 166fa9e4066Sahrens return (0); 167fa9e4066Sahrens } 168fa9e4066Sahrens 169fa9e4066Sahrens if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) { 17099653d4eSeschrock if (hdl != NULL) 17199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 172edb901aaSMarcel Telka "snapshot delimiter '@' is not expected here")); 173fa9e4066Sahrens return (0); 174fa9e4066Sahrens } 175fa9e4066Sahrens 1761d452cf5Sahrens if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) { 1771d452cf5Sahrens if (hdl != NULL) 1781d452cf5Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 179d7d4af51Smmusante "missing '@' delimiter in snapshot name")); 1801d452cf5Sahrens return (0); 1811d452cf5Sahrens } 1821d452cf5Sahrens 183edb901aaSMarcel Telka if (!(type & ZFS_TYPE_BOOKMARK) && strchr(path, '#') != NULL) { 184edb901aaSMarcel Telka if (hdl != NULL) 185edb901aaSMarcel Telka zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 186edb901aaSMarcel Telka "bookmark delimiter '#' is not expected here")); 187edb901aaSMarcel Telka return (0); 188edb901aaSMarcel Telka } 189edb901aaSMarcel Telka 190edb901aaSMarcel Telka if (type == ZFS_TYPE_BOOKMARK && strchr(path, '#') == NULL) { 191edb901aaSMarcel Telka if (hdl != NULL) 192edb901aaSMarcel Telka zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 193edb901aaSMarcel Telka "missing '#' delimiter in bookmark name")); 194edb901aaSMarcel Telka return (0); 195edb901aaSMarcel Telka } 196edb901aaSMarcel Telka 197f18faf3fSek if (modifying && strchr(path, '%') != NULL) { 198f18faf3fSek if (hdl != NULL) 199f18faf3fSek zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 200f18faf3fSek "invalid character %c in name"), '%'); 201f18faf3fSek return (0); 202f18faf3fSek } 203f18faf3fSek 20499653d4eSeschrock return (-1); 205fa9e4066Sahrens } 206fa9e4066Sahrens 207fa9e4066Sahrens int 208fa9e4066Sahrens zfs_name_valid(const char *name, zfs_type_t type) 209fa9e4066Sahrens { 210e7cbe64fSgw if (type == ZFS_TYPE_POOL) 211e7cbe64fSgw return (zpool_name_valid(NULL, B_FALSE, name)); 212f18faf3fSek return (zfs_validate_name(NULL, name, type, B_FALSE)); 213fa9e4066Sahrens } 214fa9e4066Sahrens 215e9dbad6fSeschrock /* 216e9dbad6fSeschrock * This function takes the raw DSL properties, and filters out the user-defined 217e9dbad6fSeschrock * properties into a separate nvlist. 218e9dbad6fSeschrock */ 219fac3008cSeschrock static nvlist_t * 220fac3008cSeschrock process_user_props(zfs_handle_t *zhp, nvlist_t *props) 221e9dbad6fSeschrock { 222e9dbad6fSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 223e9dbad6fSeschrock nvpair_t *elem; 224e9dbad6fSeschrock nvlist_t *propval; 225fac3008cSeschrock nvlist_t *nvl; 226e9dbad6fSeschrock 227fac3008cSeschrock if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) { 228fac3008cSeschrock (void) no_memory(hdl); 229fac3008cSeschrock return (NULL); 230fac3008cSeschrock } 231e9dbad6fSeschrock 232e9dbad6fSeschrock elem = NULL; 233fac3008cSeschrock while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 234e9dbad6fSeschrock if (!zfs_prop_user(nvpair_name(elem))) 235e9dbad6fSeschrock continue; 236e9dbad6fSeschrock 237e9dbad6fSeschrock verify(nvpair_value_nvlist(elem, &propval) == 0); 238fac3008cSeschrock if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) { 239fac3008cSeschrock nvlist_free(nvl); 240fac3008cSeschrock (void) no_memory(hdl); 241fac3008cSeschrock return (NULL); 242fac3008cSeschrock } 243e9dbad6fSeschrock } 244e9dbad6fSeschrock 245fac3008cSeschrock return (nvl); 246e9dbad6fSeschrock } 247e9dbad6fSeschrock 24829ab75c9Srm static zpool_handle_t * 24929ab75c9Srm zpool_add_handle(zfs_handle_t *zhp, const char *pool_name) 25029ab75c9Srm { 25129ab75c9Srm libzfs_handle_t *hdl = zhp->zfs_hdl; 25229ab75c9Srm zpool_handle_t *zph; 25329ab75c9Srm 25429ab75c9Srm if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) { 25529ab75c9Srm if (hdl->libzfs_pool_handles != NULL) 25629ab75c9Srm zph->zpool_next = hdl->libzfs_pool_handles; 25729ab75c9Srm hdl->libzfs_pool_handles = zph; 25829ab75c9Srm } 25929ab75c9Srm return (zph); 26029ab75c9Srm } 26129ab75c9Srm 26229ab75c9Srm static zpool_handle_t * 26329ab75c9Srm zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len) 26429ab75c9Srm { 26529ab75c9Srm libzfs_handle_t *hdl = zhp->zfs_hdl; 26629ab75c9Srm zpool_handle_t *zph = hdl->libzfs_pool_handles; 26729ab75c9Srm 26829ab75c9Srm while ((zph != NULL) && 26929ab75c9Srm (strncmp(pool_name, zpool_get_name(zph), len) != 0)) 27029ab75c9Srm zph = zph->zpool_next; 27129ab75c9Srm return (zph); 27229ab75c9Srm } 27329ab75c9Srm 27429ab75c9Srm /* 27529ab75c9Srm * Returns a handle to the pool that contains the provided dataset. 27629ab75c9Srm * If a handle to that pool already exists then that handle is returned. 27729ab75c9Srm * Otherwise, a new handle is created and added to the list of handles. 27829ab75c9Srm */ 27929ab75c9Srm static zpool_handle_t * 28029ab75c9Srm zpool_handle(zfs_handle_t *zhp) 28129ab75c9Srm { 28229ab75c9Srm char *pool_name; 28329ab75c9Srm int len; 28429ab75c9Srm zpool_handle_t *zph; 28529ab75c9Srm 28678f17100SMatthew Ahrens len = strcspn(zhp->zfs_name, "/@#") + 1; 28729ab75c9Srm pool_name = zfs_alloc(zhp->zfs_hdl, len); 28829ab75c9Srm (void) strlcpy(pool_name, zhp->zfs_name, len); 28929ab75c9Srm 29029ab75c9Srm zph = zpool_find_handle(zhp, pool_name, len); 29129ab75c9Srm if (zph == NULL) 29229ab75c9Srm zph = zpool_add_handle(zhp, pool_name); 29329ab75c9Srm 29429ab75c9Srm free(pool_name); 29529ab75c9Srm return (zph); 29629ab75c9Srm } 29729ab75c9Srm 29829ab75c9Srm void 29929ab75c9Srm zpool_free_handles(libzfs_handle_t *hdl) 30029ab75c9Srm { 30129ab75c9Srm zpool_handle_t *next, *zph = hdl->libzfs_pool_handles; 30229ab75c9Srm 30329ab75c9Srm while (zph != NULL) { 30429ab75c9Srm next = zph->zpool_next; 30529ab75c9Srm zpool_close(zph); 30629ab75c9Srm zph = next; 30729ab75c9Srm } 30829ab75c9Srm hdl->libzfs_pool_handles = NULL; 30929ab75c9Srm } 31029ab75c9Srm 311fa9e4066Sahrens /* 312fa9e4066Sahrens * Utility function to gather stats (objset and zpl) for the given object. 313fa9e4066Sahrens */ 314fa9e4066Sahrens static int 315ebedde84SEric Taylor get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc) 316fa9e4066Sahrens { 317e9dbad6fSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 318fa9e4066Sahrens 319ebedde84SEric Taylor (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name)); 320fa9e4066Sahrens 321ebedde84SEric Taylor while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) { 3227f7322feSeschrock if (errno == ENOMEM) { 323ebedde84SEric Taylor if (zcmd_expand_dst_nvlist(hdl, zc) != 0) { 32499653d4eSeschrock return (-1); 325e9dbad6fSeschrock } 3267f7322feSeschrock } else { 3277f7322feSeschrock return (-1); 3287f7322feSeschrock } 3297f7322feSeschrock } 330ebedde84SEric Taylor return (0); 331ebedde84SEric Taylor } 332fa9e4066Sahrens 33392241e0bSTom Erickson /* 33492241e0bSTom Erickson * Utility function to get the received properties of the given object. 33592241e0bSTom Erickson */ 33692241e0bSTom Erickson static int 33792241e0bSTom Erickson get_recvd_props_ioctl(zfs_handle_t *zhp) 33892241e0bSTom Erickson { 33992241e0bSTom Erickson libzfs_handle_t *hdl = zhp->zfs_hdl; 34092241e0bSTom Erickson nvlist_t *recvdprops; 34192241e0bSTom Erickson zfs_cmd_t zc = { 0 }; 34292241e0bSTom Erickson int err; 34392241e0bSTom Erickson 34492241e0bSTom Erickson if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) 34592241e0bSTom Erickson return (-1); 34692241e0bSTom Erickson 34792241e0bSTom Erickson (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 34892241e0bSTom Erickson 34992241e0bSTom Erickson while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) { 35092241e0bSTom Erickson if (errno == ENOMEM) { 35192241e0bSTom Erickson if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 35292241e0bSTom Erickson return (-1); 35392241e0bSTom Erickson } 35492241e0bSTom Erickson } else { 35592241e0bSTom Erickson zcmd_free_nvlists(&zc); 35692241e0bSTom Erickson return (-1); 35792241e0bSTom Erickson } 35892241e0bSTom Erickson } 35992241e0bSTom Erickson 36092241e0bSTom Erickson err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops); 36192241e0bSTom Erickson zcmd_free_nvlists(&zc); 36292241e0bSTom Erickson if (err != 0) 36392241e0bSTom Erickson return (-1); 36492241e0bSTom Erickson 36592241e0bSTom Erickson nvlist_free(zhp->zfs_recvd_props); 36692241e0bSTom Erickson zhp->zfs_recvd_props = recvdprops; 36792241e0bSTom Erickson 36892241e0bSTom Erickson return (0); 36992241e0bSTom Erickson } 37092241e0bSTom Erickson 371ebedde84SEric Taylor static int 372ebedde84SEric Taylor put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc) 373ebedde84SEric Taylor { 374ebedde84SEric Taylor nvlist_t *allprops, *userprops; 375fa9e4066Sahrens 376ebedde84SEric Taylor zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */ 377ebedde84SEric Taylor 378ebedde84SEric Taylor if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) { 37999653d4eSeschrock return (-1); 38099653d4eSeschrock } 381fa9e4066Sahrens 38214843421SMatthew Ahrens /* 38314843421SMatthew Ahrens * XXX Why do we store the user props separately, in addition to 38414843421SMatthew Ahrens * storing them in zfs_props? 38514843421SMatthew Ahrens */ 386fac3008cSeschrock if ((userprops = process_user_props(zhp, allprops)) == NULL) { 387fac3008cSeschrock nvlist_free(allprops); 388e9dbad6fSeschrock return (-1); 389fac3008cSeschrock } 390fac3008cSeschrock 391fac3008cSeschrock nvlist_free(zhp->zfs_props); 392fac3008cSeschrock nvlist_free(zhp->zfs_user_props); 393fac3008cSeschrock 394fac3008cSeschrock zhp->zfs_props = allprops; 395fac3008cSeschrock zhp->zfs_user_props = userprops; 39699653d4eSeschrock 397fa9e4066Sahrens return (0); 398fa9e4066Sahrens } 399fa9e4066Sahrens 400ebedde84SEric Taylor static int 401ebedde84SEric Taylor get_stats(zfs_handle_t *zhp) 402ebedde84SEric Taylor { 403ebedde84SEric Taylor int rc = 0; 404ebedde84SEric Taylor zfs_cmd_t zc = { 0 }; 405ebedde84SEric Taylor 406ebedde84SEric Taylor if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 407ebedde84SEric Taylor return (-1); 408ebedde84SEric Taylor if (get_stats_ioctl(zhp, &zc) != 0) 409ebedde84SEric Taylor rc = -1; 410ebedde84SEric Taylor else if (put_stats_zhdl(zhp, &zc) != 0) 411ebedde84SEric Taylor rc = -1; 412ebedde84SEric Taylor zcmd_free_nvlists(&zc); 413ebedde84SEric Taylor return (rc); 414ebedde84SEric Taylor } 415ebedde84SEric Taylor 416fa9e4066Sahrens /* 417fa9e4066Sahrens * Refresh the properties currently stored in the handle. 418fa9e4066Sahrens */ 419fa9e4066Sahrens void 420fa9e4066Sahrens zfs_refresh_properties(zfs_handle_t *zhp) 421fa9e4066Sahrens { 422fa9e4066Sahrens (void) get_stats(zhp); 423fa9e4066Sahrens } 424fa9e4066Sahrens 425fa9e4066Sahrens /* 426fa9e4066Sahrens * Makes a handle from the given dataset name. Used by zfs_open() and 427fa9e4066Sahrens * zfs_iter_* to create child handles on the fly. 428fa9e4066Sahrens */ 429ebedde84SEric Taylor static int 430ebedde84SEric Taylor make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc) 431fa9e4066Sahrens { 432503ad85cSMatthew Ahrens if (put_stats_zhdl(zhp, zc) != 0) 433ebedde84SEric Taylor return (-1); 43431fd60d3Sahrens 435fa9e4066Sahrens /* 436fa9e4066Sahrens * We've managed to open the dataset and gather statistics. Determine 437fa9e4066Sahrens * the high-level type. 438fa9e4066Sahrens */ 439a2eea2e1Sahrens if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) 440a2eea2e1Sahrens zhp->zfs_head_type = ZFS_TYPE_VOLUME; 441a2eea2e1Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) 442a2eea2e1Sahrens zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM; 443*e0f1c0afSOlaf Faaland else if (zhp->zfs_dmustats.dds_type == DMU_OST_OTHER) 444*e0f1c0afSOlaf Faaland return (-1); 445a2eea2e1Sahrens else 446a2eea2e1Sahrens abort(); 447a2eea2e1Sahrens 448fa9e4066Sahrens if (zhp->zfs_dmustats.dds_is_snapshot) 449fa9e4066Sahrens zhp->zfs_type = ZFS_TYPE_SNAPSHOT; 450fa9e4066Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) 451fa9e4066Sahrens zhp->zfs_type = ZFS_TYPE_VOLUME; 452fa9e4066Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) 453fa9e4066Sahrens zhp->zfs_type = ZFS_TYPE_FILESYSTEM; 454fa9e4066Sahrens else 45599653d4eSeschrock abort(); /* we should never see any other types */ 456fa9e4066Sahrens 4579fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL) 4589fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States return (-1); 4599fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 460ebedde84SEric Taylor return (0); 461ebedde84SEric Taylor } 462ebedde84SEric Taylor 463ebedde84SEric Taylor zfs_handle_t * 464ebedde84SEric Taylor make_dataset_handle(libzfs_handle_t *hdl, const char *path) 465ebedde84SEric Taylor { 466ebedde84SEric Taylor zfs_cmd_t zc = { 0 }; 467ebedde84SEric Taylor 468ebedde84SEric Taylor zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 469ebedde84SEric Taylor 470ebedde84SEric Taylor if (zhp == NULL) 471ebedde84SEric Taylor return (NULL); 472ebedde84SEric Taylor 473ebedde84SEric Taylor zhp->zfs_hdl = hdl; 474ebedde84SEric Taylor (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name)); 475ebedde84SEric Taylor if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) { 476ebedde84SEric Taylor free(zhp); 477ebedde84SEric Taylor return (NULL); 478ebedde84SEric Taylor } 479ebedde84SEric Taylor if (get_stats_ioctl(zhp, &zc) == -1) { 480ebedde84SEric Taylor zcmd_free_nvlists(&zc); 481ebedde84SEric Taylor free(zhp); 482ebedde84SEric Taylor return (NULL); 483ebedde84SEric Taylor } 484ebedde84SEric Taylor if (make_dataset_handle_common(zhp, &zc) == -1) { 485ebedde84SEric Taylor free(zhp); 486ebedde84SEric Taylor zhp = NULL; 487ebedde84SEric Taylor } 488ebedde84SEric Taylor zcmd_free_nvlists(&zc); 489ebedde84SEric Taylor return (zhp); 490ebedde84SEric Taylor } 491ebedde84SEric Taylor 49219b94df9SMatthew Ahrens zfs_handle_t * 493ebedde84SEric Taylor make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc) 494ebedde84SEric Taylor { 495ebedde84SEric Taylor zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 496ebedde84SEric Taylor 497ebedde84SEric Taylor if (zhp == NULL) 498ebedde84SEric Taylor return (NULL); 499ebedde84SEric Taylor 500ebedde84SEric Taylor zhp->zfs_hdl = hdl; 501ebedde84SEric Taylor (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name)); 502ebedde84SEric Taylor if (make_dataset_handle_common(zhp, zc) == -1) { 503ebedde84SEric Taylor free(zhp); 504ebedde84SEric Taylor return (NULL); 505ebedde84SEric Taylor } 506fa9e4066Sahrens return (zhp); 507fa9e4066Sahrens } 508fa9e4066Sahrens 5090d8fa8f8SMartin Matuska zfs_handle_t * 5100d8fa8f8SMartin Matuska make_dataset_simple_handle_zc(zfs_handle_t *pzhp, zfs_cmd_t *zc) 5110d8fa8f8SMartin Matuska { 5120d8fa8f8SMartin Matuska zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 5130d8fa8f8SMartin Matuska 5140d8fa8f8SMartin Matuska if (zhp == NULL) 5150d8fa8f8SMartin Matuska return (NULL); 5160d8fa8f8SMartin Matuska 5170d8fa8f8SMartin Matuska zhp->zfs_hdl = pzhp->zfs_hdl; 5180d8fa8f8SMartin Matuska (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name)); 5190d8fa8f8SMartin Matuska zhp->zfs_head_type = pzhp->zfs_type; 5200d8fa8f8SMartin Matuska zhp->zfs_type = ZFS_TYPE_SNAPSHOT; 5210d8fa8f8SMartin Matuska zhp->zpool_hdl = zpool_handle(zhp); 5220d8fa8f8SMartin Matuska return (zhp); 5230d8fa8f8SMartin Matuska } 5240d8fa8f8SMartin Matuska 52519b94df9SMatthew Ahrens zfs_handle_t * 52619b94df9SMatthew Ahrens zfs_handle_dup(zfs_handle_t *zhp_orig) 52719b94df9SMatthew Ahrens { 52819b94df9SMatthew Ahrens zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 52919b94df9SMatthew Ahrens 53019b94df9SMatthew Ahrens if (zhp == NULL) 53119b94df9SMatthew Ahrens return (NULL); 53219b94df9SMatthew Ahrens 53319b94df9SMatthew Ahrens zhp->zfs_hdl = zhp_orig->zfs_hdl; 53419b94df9SMatthew Ahrens zhp->zpool_hdl = zhp_orig->zpool_hdl; 53519b94df9SMatthew Ahrens (void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name, 53619b94df9SMatthew Ahrens sizeof (zhp->zfs_name)); 53719b94df9SMatthew Ahrens zhp->zfs_type = zhp_orig->zfs_type; 53819b94df9SMatthew Ahrens zhp->zfs_head_type = zhp_orig->zfs_head_type; 53919b94df9SMatthew Ahrens zhp->zfs_dmustats = zhp_orig->zfs_dmustats; 54019b94df9SMatthew Ahrens if (zhp_orig->zfs_props != NULL) { 54119b94df9SMatthew Ahrens if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) { 54219b94df9SMatthew Ahrens (void) no_memory(zhp->zfs_hdl); 54319b94df9SMatthew Ahrens zfs_close(zhp); 54419b94df9SMatthew Ahrens return (NULL); 54519b94df9SMatthew Ahrens } 54619b94df9SMatthew Ahrens } 54719b94df9SMatthew Ahrens if (zhp_orig->zfs_user_props != NULL) { 54819b94df9SMatthew Ahrens if (nvlist_dup(zhp_orig->zfs_user_props, 54919b94df9SMatthew Ahrens &zhp->zfs_user_props, 0) != 0) { 55019b94df9SMatthew Ahrens (void) no_memory(zhp->zfs_hdl); 55119b94df9SMatthew Ahrens zfs_close(zhp); 55219b94df9SMatthew Ahrens return (NULL); 55319b94df9SMatthew Ahrens } 55419b94df9SMatthew Ahrens } 55519b94df9SMatthew Ahrens if (zhp_orig->zfs_recvd_props != NULL) { 55619b94df9SMatthew Ahrens if (nvlist_dup(zhp_orig->zfs_recvd_props, 55719b94df9SMatthew Ahrens &zhp->zfs_recvd_props, 0)) { 55819b94df9SMatthew Ahrens (void) no_memory(zhp->zfs_hdl); 55919b94df9SMatthew Ahrens zfs_close(zhp); 56019b94df9SMatthew Ahrens return (NULL); 56119b94df9SMatthew Ahrens } 56219b94df9SMatthew Ahrens } 56319b94df9SMatthew Ahrens zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck; 56419b94df9SMatthew Ahrens if (zhp_orig->zfs_mntopts != NULL) { 56519b94df9SMatthew Ahrens zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl, 56619b94df9SMatthew Ahrens zhp_orig->zfs_mntopts); 56719b94df9SMatthew Ahrens } 56819b94df9SMatthew Ahrens zhp->zfs_props_table = zhp_orig->zfs_props_table; 56919b94df9SMatthew Ahrens return (zhp); 57019b94df9SMatthew Ahrens } 57119b94df9SMatthew Ahrens 57278f17100SMatthew Ahrens boolean_t 57378f17100SMatthew Ahrens zfs_bookmark_exists(const char *path) 57478f17100SMatthew Ahrens { 57578f17100SMatthew Ahrens nvlist_t *bmarks; 57678f17100SMatthew Ahrens nvlist_t *props; 5779adfa60dSMatthew Ahrens char fsname[ZFS_MAX_DATASET_NAME_LEN]; 57878f17100SMatthew Ahrens char *bmark_name; 57978f17100SMatthew Ahrens char *pound; 58078f17100SMatthew Ahrens int err; 58178f17100SMatthew Ahrens boolean_t rv; 58278f17100SMatthew Ahrens 58378f17100SMatthew Ahrens 58478f17100SMatthew Ahrens (void) strlcpy(fsname, path, sizeof (fsname)); 58578f17100SMatthew Ahrens pound = strchr(fsname, '#'); 58678f17100SMatthew Ahrens if (pound == NULL) 58778f17100SMatthew Ahrens return (B_FALSE); 58878f17100SMatthew Ahrens 58978f17100SMatthew Ahrens *pound = '\0'; 59078f17100SMatthew Ahrens bmark_name = pound + 1; 59178f17100SMatthew Ahrens props = fnvlist_alloc(); 59278f17100SMatthew Ahrens err = lzc_get_bookmarks(fsname, props, &bmarks); 59378f17100SMatthew Ahrens nvlist_free(props); 59478f17100SMatthew Ahrens if (err != 0) { 59578f17100SMatthew Ahrens nvlist_free(bmarks); 59678f17100SMatthew Ahrens return (B_FALSE); 59778f17100SMatthew Ahrens } 59878f17100SMatthew Ahrens 59978f17100SMatthew Ahrens rv = nvlist_exists(bmarks, bmark_name); 60078f17100SMatthew Ahrens nvlist_free(bmarks); 60178f17100SMatthew Ahrens return (rv); 60278f17100SMatthew Ahrens } 60378f17100SMatthew Ahrens 60478f17100SMatthew Ahrens zfs_handle_t * 60578f17100SMatthew Ahrens make_bookmark_handle(zfs_handle_t *parent, const char *path, 60678f17100SMatthew Ahrens nvlist_t *bmark_props) 60778f17100SMatthew Ahrens { 60878f17100SMatthew Ahrens zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 60978f17100SMatthew Ahrens 61078f17100SMatthew Ahrens if (zhp == NULL) 61178f17100SMatthew Ahrens return (NULL); 61278f17100SMatthew Ahrens 61378f17100SMatthew Ahrens /* Fill in the name. */ 61478f17100SMatthew Ahrens zhp->zfs_hdl = parent->zfs_hdl; 61578f17100SMatthew Ahrens (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name)); 61678f17100SMatthew Ahrens 61778f17100SMatthew Ahrens /* Set the property lists. */ 61878f17100SMatthew Ahrens if (nvlist_dup(bmark_props, &zhp->zfs_props, 0) != 0) { 61978f17100SMatthew Ahrens free(zhp); 62078f17100SMatthew Ahrens return (NULL); 62178f17100SMatthew Ahrens } 62278f17100SMatthew Ahrens 62378f17100SMatthew Ahrens /* Set the types. */ 62478f17100SMatthew Ahrens zhp->zfs_head_type = parent->zfs_head_type; 62578f17100SMatthew Ahrens zhp->zfs_type = ZFS_TYPE_BOOKMARK; 62678f17100SMatthew Ahrens 62778f17100SMatthew Ahrens if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL) { 62878f17100SMatthew Ahrens nvlist_free(zhp->zfs_props); 62978f17100SMatthew Ahrens free(zhp); 63078f17100SMatthew Ahrens return (NULL); 63178f17100SMatthew Ahrens } 63278f17100SMatthew Ahrens 63378f17100SMatthew Ahrens return (zhp); 63478f17100SMatthew Ahrens } 63578f17100SMatthew Ahrens 636edb901aaSMarcel Telka struct zfs_open_bookmarks_cb_data { 637edb901aaSMarcel Telka const char *path; 638edb901aaSMarcel Telka zfs_handle_t *zhp; 639edb901aaSMarcel Telka }; 640edb901aaSMarcel Telka 641edb901aaSMarcel Telka static int 642edb901aaSMarcel Telka zfs_open_bookmarks_cb(zfs_handle_t *zhp, void *data) 643edb901aaSMarcel Telka { 644edb901aaSMarcel Telka struct zfs_open_bookmarks_cb_data *dp = data; 645edb901aaSMarcel Telka 646edb901aaSMarcel Telka /* 647edb901aaSMarcel Telka * Is it the one we are looking for? 648edb901aaSMarcel Telka */ 649edb901aaSMarcel Telka if (strcmp(dp->path, zfs_get_name(zhp)) == 0) { 650edb901aaSMarcel Telka /* 651edb901aaSMarcel Telka * We found it. Save it and let the caller know we are done. 652edb901aaSMarcel Telka */ 653edb901aaSMarcel Telka dp->zhp = zhp; 654edb901aaSMarcel Telka return (EEXIST); 655edb901aaSMarcel Telka } 656edb901aaSMarcel Telka 657edb901aaSMarcel Telka /* 658edb901aaSMarcel Telka * Not found. Close the handle and ask for another one. 659edb901aaSMarcel Telka */ 660edb901aaSMarcel Telka zfs_close(zhp); 661edb901aaSMarcel Telka return (0); 662edb901aaSMarcel Telka } 663edb901aaSMarcel Telka 664fa9e4066Sahrens /* 665edb901aaSMarcel Telka * Opens the given snapshot, bookmark, filesystem, or volume. The 'types' 666fa9e4066Sahrens * argument is a mask of acceptable types. The function will print an 667fa9e4066Sahrens * appropriate error message and return NULL if it can't be opened. 668fa9e4066Sahrens */ 669fa9e4066Sahrens zfs_handle_t * 67099653d4eSeschrock zfs_open(libzfs_handle_t *hdl, const char *path, int types) 671fa9e4066Sahrens { 672fa9e4066Sahrens zfs_handle_t *zhp; 67399653d4eSeschrock char errbuf[1024]; 674edb901aaSMarcel Telka char *bookp; 67599653d4eSeschrock 67699653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), 67799653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), path); 678fa9e4066Sahrens 679fa9e4066Sahrens /* 68099653d4eSeschrock * Validate the name before we even try to open it. 681fa9e4066Sahrens */ 682edb901aaSMarcel Telka if (!zfs_validate_name(hdl, path, types, B_FALSE)) { 68399653d4eSeschrock (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 684fa9e4066Sahrens return (NULL); 685fa9e4066Sahrens } 686fa9e4066Sahrens 687fa9e4066Sahrens /* 688edb901aaSMarcel Telka * Bookmarks needs to be handled separately. 689fa9e4066Sahrens */ 690edb901aaSMarcel Telka bookp = strchr(path, '#'); 691edb901aaSMarcel Telka if (bookp == NULL) { 692edb901aaSMarcel Telka /* 693edb901aaSMarcel Telka * Try to get stats for the dataset, which will tell us if it 694edb901aaSMarcel Telka * exists. 695edb901aaSMarcel Telka */ 696edb901aaSMarcel Telka errno = 0; 697edb901aaSMarcel Telka if ((zhp = make_dataset_handle(hdl, path)) == NULL) { 698edb901aaSMarcel Telka (void) zfs_standard_error(hdl, errno, errbuf); 699edb901aaSMarcel Telka return (NULL); 700edb901aaSMarcel Telka } 701edb901aaSMarcel Telka } else { 702edb901aaSMarcel Telka char dsname[ZFS_MAX_DATASET_NAME_LEN]; 703edb901aaSMarcel Telka zfs_handle_t *pzhp; 704edb901aaSMarcel Telka struct zfs_open_bookmarks_cb_data cb_data = {path, NULL}; 705edb901aaSMarcel Telka 706edb901aaSMarcel Telka /* 707edb901aaSMarcel Telka * We need to cut out '#' and everything after '#' 708edb901aaSMarcel Telka * to get the parent dataset name only. 709edb901aaSMarcel Telka */ 710edb901aaSMarcel Telka assert(bookp - path < sizeof (dsname)); 711edb901aaSMarcel Telka (void) strncpy(dsname, path, bookp - path); 712edb901aaSMarcel Telka dsname[bookp - path] = '\0'; 713edb901aaSMarcel Telka 714edb901aaSMarcel Telka /* 715edb901aaSMarcel Telka * Create handle for the parent dataset. 716edb901aaSMarcel Telka */ 717edb901aaSMarcel Telka errno = 0; 718edb901aaSMarcel Telka if ((pzhp = make_dataset_handle(hdl, dsname)) == NULL) { 719edb901aaSMarcel Telka (void) zfs_standard_error(hdl, errno, errbuf); 720edb901aaSMarcel Telka return (NULL); 721edb901aaSMarcel Telka } 722edb901aaSMarcel Telka 723edb901aaSMarcel Telka /* 724edb901aaSMarcel Telka * Iterate bookmarks to find the right one. 725edb901aaSMarcel Telka */ 726edb901aaSMarcel Telka errno = 0; 727edb901aaSMarcel Telka if ((zfs_iter_bookmarks(pzhp, zfs_open_bookmarks_cb, 728edb901aaSMarcel Telka &cb_data) == 0) && (cb_data.zhp == NULL)) { 729edb901aaSMarcel Telka (void) zfs_error(hdl, EZFS_NOENT, errbuf); 730edb901aaSMarcel Telka zfs_close(pzhp); 731edb901aaSMarcel Telka return (NULL); 732edb901aaSMarcel Telka } 733edb901aaSMarcel Telka if (cb_data.zhp == NULL) { 734edb901aaSMarcel Telka (void) zfs_standard_error(hdl, errno, errbuf); 735edb901aaSMarcel Telka zfs_close(pzhp); 736edb901aaSMarcel Telka return (NULL); 737edb901aaSMarcel Telka } 738edb901aaSMarcel Telka zhp = cb_data.zhp; 739edb901aaSMarcel Telka 740edb901aaSMarcel Telka /* 741edb901aaSMarcel Telka * Cleanup. 742edb901aaSMarcel Telka */ 743edb901aaSMarcel Telka zfs_close(pzhp); 744fa9e4066Sahrens } 745fa9e4066Sahrens 746fa9e4066Sahrens if (!(types & zhp->zfs_type)) { 74799653d4eSeschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 74894de1d4cSeschrock zfs_close(zhp); 749fa9e4066Sahrens return (NULL); 750fa9e4066Sahrens } 751fa9e4066Sahrens 752fa9e4066Sahrens return (zhp); 753fa9e4066Sahrens } 754fa9e4066Sahrens 755fa9e4066Sahrens /* 756fa9e4066Sahrens * Release a ZFS handle. Nothing to do but free the associated memory. 757fa9e4066Sahrens */ 758fa9e4066Sahrens void 759fa9e4066Sahrens zfs_close(zfs_handle_t *zhp) 760fa9e4066Sahrens { 761fa9e4066Sahrens if (zhp->zfs_mntopts) 762fa9e4066Sahrens free(zhp->zfs_mntopts); 763e9dbad6fSeschrock nvlist_free(zhp->zfs_props); 764e9dbad6fSeschrock nvlist_free(zhp->zfs_user_props); 76592241e0bSTom Erickson nvlist_free(zhp->zfs_recvd_props); 766fa9e4066Sahrens free(zhp); 767fa9e4066Sahrens } 768fa9e4066Sahrens 769ebedde84SEric Taylor typedef struct mnttab_node { 770ebedde84SEric Taylor struct mnttab mtn_mt; 771ebedde84SEric Taylor avl_node_t mtn_node; 772ebedde84SEric Taylor } mnttab_node_t; 773ebedde84SEric Taylor 774ebedde84SEric Taylor static int 775ebedde84SEric Taylor libzfs_mnttab_cache_compare(const void *arg1, const void *arg2) 776ebedde84SEric Taylor { 777ebedde84SEric Taylor const mnttab_node_t *mtn1 = arg1; 778ebedde84SEric Taylor const mnttab_node_t *mtn2 = arg2; 779ebedde84SEric Taylor int rv; 780ebedde84SEric Taylor 781ebedde84SEric Taylor rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special); 782ebedde84SEric Taylor 783ebedde84SEric Taylor if (rv == 0) 784ebedde84SEric Taylor return (0); 785ebedde84SEric Taylor return (rv > 0 ? 1 : -1); 786ebedde84SEric Taylor } 787ebedde84SEric Taylor 788ebedde84SEric Taylor void 789ebedde84SEric Taylor libzfs_mnttab_init(libzfs_handle_t *hdl) 790ebedde84SEric Taylor { 791591e0e13SSebastien Roy (void) mutex_init(&hdl->libzfs_mnttab_cache_lock, 792591e0e13SSebastien Roy LOCK_NORMAL | LOCK_ERRORCHECK, NULL); 793ebedde84SEric Taylor assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0); 794ebedde84SEric Taylor avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare, 795ebedde84SEric Taylor sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node)); 796b2634b9cSEric Taylor } 797b2634b9cSEric Taylor 798b2634b9cSEric Taylor void 799b2634b9cSEric Taylor libzfs_mnttab_update(libzfs_handle_t *hdl) 800b2634b9cSEric Taylor { 801b2634b9cSEric Taylor struct mnttab entry; 802ebedde84SEric Taylor 803ebedde84SEric Taylor rewind(hdl->libzfs_mnttab); 804ebedde84SEric Taylor while (getmntent(hdl->libzfs_mnttab, &entry) == 0) { 805ebedde84SEric Taylor mnttab_node_t *mtn; 806ebedde84SEric Taylor 807ebedde84SEric Taylor if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) 808ebedde84SEric Taylor continue; 809ebedde84SEric Taylor mtn = zfs_alloc(hdl, sizeof (mnttab_node_t)); 810ebedde84SEric Taylor mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special); 811ebedde84SEric Taylor mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp); 812ebedde84SEric Taylor mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype); 813ebedde84SEric Taylor mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts); 814ebedde84SEric Taylor avl_add(&hdl->libzfs_mnttab_cache, mtn); 815ebedde84SEric Taylor } 816ebedde84SEric Taylor } 817ebedde84SEric Taylor 818ebedde84SEric Taylor void 819ebedde84SEric Taylor libzfs_mnttab_fini(libzfs_handle_t *hdl) 820ebedde84SEric Taylor { 821ebedde84SEric Taylor void *cookie = NULL; 822ebedde84SEric Taylor mnttab_node_t *mtn; 823ebedde84SEric Taylor 82488f61deeSIgor Kozhukhov while ((mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie)) 82588f61deeSIgor Kozhukhov != NULL) { 826ebedde84SEric Taylor free(mtn->mtn_mt.mnt_special); 827ebedde84SEric Taylor free(mtn->mtn_mt.mnt_mountp); 828ebedde84SEric Taylor free(mtn->mtn_mt.mnt_fstype); 829ebedde84SEric Taylor free(mtn->mtn_mt.mnt_mntopts); 830ebedde84SEric Taylor free(mtn); 831ebedde84SEric Taylor } 832ebedde84SEric Taylor avl_destroy(&hdl->libzfs_mnttab_cache); 833591e0e13SSebastien Roy (void) mutex_destroy(&hdl->libzfs_mnttab_cache_lock); 834ebedde84SEric Taylor } 835ebedde84SEric Taylor 836b2634b9cSEric Taylor void 837b2634b9cSEric Taylor libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable) 838b2634b9cSEric Taylor { 839b2634b9cSEric Taylor hdl->libzfs_mnttab_enable = enable; 840b2634b9cSEric Taylor } 841b2634b9cSEric Taylor 842ebedde84SEric Taylor int 843ebedde84SEric Taylor libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname, 844ebedde84SEric Taylor struct mnttab *entry) 845ebedde84SEric Taylor { 846ebedde84SEric Taylor mnttab_node_t find; 847ebedde84SEric Taylor mnttab_node_t *mtn; 848591e0e13SSebastien Roy int ret = ENOENT; 849ebedde84SEric Taylor 850b2634b9cSEric Taylor if (!hdl->libzfs_mnttab_enable) { 851b2634b9cSEric Taylor struct mnttab srch = { 0 }; 852b2634b9cSEric Taylor 853b2634b9cSEric Taylor if (avl_numnodes(&hdl->libzfs_mnttab_cache)) 854b2634b9cSEric Taylor libzfs_mnttab_fini(hdl); 855b2634b9cSEric Taylor rewind(hdl->libzfs_mnttab); 856b2634b9cSEric Taylor srch.mnt_special = (char *)fsname; 857b2634b9cSEric Taylor srch.mnt_fstype = MNTTYPE_ZFS; 858b2634b9cSEric Taylor if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0) 859b2634b9cSEric Taylor return (0); 860b2634b9cSEric Taylor else 861b2634b9cSEric Taylor return (ENOENT); 862b2634b9cSEric Taylor } 863b2634b9cSEric Taylor 864591e0e13SSebastien Roy mutex_enter(&hdl->libzfs_mnttab_cache_lock); 865ebedde84SEric Taylor if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0) 866b2634b9cSEric Taylor libzfs_mnttab_update(hdl); 867ebedde84SEric Taylor 868ebedde84SEric Taylor find.mtn_mt.mnt_special = (char *)fsname; 869ebedde84SEric Taylor mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL); 870ebedde84SEric Taylor if (mtn) { 871ebedde84SEric Taylor *entry = mtn->mtn_mt; 872591e0e13SSebastien Roy ret = 0; 873ebedde84SEric Taylor } 874591e0e13SSebastien Roy mutex_exit(&hdl->libzfs_mnttab_cache_lock); 875591e0e13SSebastien Roy return (ret); 876ebedde84SEric Taylor } 877ebedde84SEric Taylor 878ebedde84SEric Taylor void 879ebedde84SEric Taylor libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special, 880ebedde84SEric Taylor const char *mountp, const char *mntopts) 881ebedde84SEric Taylor { 882ebedde84SEric Taylor mnttab_node_t *mtn; 883ebedde84SEric Taylor 884591e0e13SSebastien Roy mutex_enter(&hdl->libzfs_mnttab_cache_lock); 885591e0e13SSebastien Roy if (avl_numnodes(&hdl->libzfs_mnttab_cache) != 0) { 886591e0e13SSebastien Roy mtn = zfs_alloc(hdl, sizeof (mnttab_node_t)); 887591e0e13SSebastien Roy mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special); 888591e0e13SSebastien Roy mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp); 889591e0e13SSebastien Roy mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS); 890591e0e13SSebastien Roy mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts); 891591e0e13SSebastien Roy avl_add(&hdl->libzfs_mnttab_cache, mtn); 892591e0e13SSebastien Roy } 893591e0e13SSebastien Roy mutex_exit(&hdl->libzfs_mnttab_cache_lock); 894ebedde84SEric Taylor } 895ebedde84SEric Taylor 896ebedde84SEric Taylor void 897ebedde84SEric Taylor libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname) 898ebedde84SEric Taylor { 899ebedde84SEric Taylor mnttab_node_t find; 900ebedde84SEric Taylor mnttab_node_t *ret; 901ebedde84SEric Taylor 902591e0e13SSebastien Roy mutex_enter(&hdl->libzfs_mnttab_cache_lock); 903ebedde84SEric Taylor find.mtn_mt.mnt_special = (char *)fsname; 90488f61deeSIgor Kozhukhov if ((ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL)) 90588f61deeSIgor Kozhukhov != NULL) { 906ebedde84SEric Taylor avl_remove(&hdl->libzfs_mnttab_cache, ret); 907ebedde84SEric Taylor free(ret->mtn_mt.mnt_special); 908ebedde84SEric Taylor free(ret->mtn_mt.mnt_mountp); 909ebedde84SEric Taylor free(ret->mtn_mt.mnt_fstype); 910ebedde84SEric Taylor free(ret->mtn_mt.mnt_mntopts); 911ebedde84SEric Taylor free(ret); 912ebedde84SEric Taylor } 913591e0e13SSebastien Roy mutex_exit(&hdl->libzfs_mnttab_cache_lock); 914ebedde84SEric Taylor } 915ebedde84SEric Taylor 9167b97dc1aSrm int 9177b97dc1aSrm zfs_spa_version(zfs_handle_t *zhp, int *spa_version) 9187b97dc1aSrm { 91929ab75c9Srm zpool_handle_t *zpool_handle = zhp->zpool_hdl; 9207b97dc1aSrm 9217b97dc1aSrm if (zpool_handle == NULL) 9227b97dc1aSrm return (-1); 9237b97dc1aSrm 9247b97dc1aSrm *spa_version = zpool_get_prop_int(zpool_handle, 9257b97dc1aSrm ZPOOL_PROP_VERSION, NULL); 9267b97dc1aSrm return (0); 9277b97dc1aSrm } 9287b97dc1aSrm 9297b97dc1aSrm /* 9307b97dc1aSrm * The choice of reservation property depends on the SPA version. 9317b97dc1aSrm */ 9327b97dc1aSrm static int 9337b97dc1aSrm zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop) 9347b97dc1aSrm { 9357b97dc1aSrm int spa_version; 9367b97dc1aSrm 9377b97dc1aSrm if (zfs_spa_version(zhp, &spa_version) < 0) 9387b97dc1aSrm return (-1); 9397b97dc1aSrm 9407b97dc1aSrm if (spa_version >= SPA_VERSION_REFRESERVATION) 9417b97dc1aSrm *resv_prop = ZFS_PROP_REFRESERVATION; 9427b97dc1aSrm else 9437b97dc1aSrm *resv_prop = ZFS_PROP_RESERVATION; 9447b97dc1aSrm 9457b97dc1aSrm return (0); 9467b97dc1aSrm } 9477b97dc1aSrm 948e9dbad6fSeschrock /* 949e9dbad6fSeschrock * Given an nvlist of properties to set, validates that they are correct, and 950e9dbad6fSeschrock * parses any numeric properties (index, boolean, etc) if they are specified as 951e9dbad6fSeschrock * strings. 952e9dbad6fSeschrock */ 9530a48a24eStimh nvlist_t * 9540a48a24eStimh zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl, 955e9316f76SJoe Stein uint64_t zoned, zfs_handle_t *zhp, zpool_handle_t *zpool_hdl, 956e9316f76SJoe Stein const char *errbuf) 957e9dbad6fSeschrock { 958e9dbad6fSeschrock nvpair_t *elem; 959e9dbad6fSeschrock uint64_t intval; 960e9dbad6fSeschrock char *strval; 961990b4856Slling zfs_prop_t prop; 962e9dbad6fSeschrock nvlist_t *ret; 963da6c28aaSamw int chosen_normal = -1; 964da6c28aaSamw int chosen_utf = -1; 965e9dbad6fSeschrock 966990b4856Slling if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) { 967990b4856Slling (void) no_memory(hdl); 968990b4856Slling return (NULL); 969e9dbad6fSeschrock } 970e9dbad6fSeschrock 97114843421SMatthew Ahrens /* 97214843421SMatthew Ahrens * Make sure this property is valid and applies to this type. 97314843421SMatthew Ahrens */ 97414843421SMatthew Ahrens 975e9dbad6fSeschrock elem = NULL; 976e9dbad6fSeschrock while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { 977990b4856Slling const char *propname = nvpair_name(elem); 978e9dbad6fSeschrock 97914843421SMatthew Ahrens prop = zfs_name_to_prop(propname); 98014843421SMatthew Ahrens if (prop == ZPROP_INVAL && zfs_prop_user(propname)) { 981990b4856Slling /* 98214843421SMatthew Ahrens * This is a user property: make sure it's a 983990b4856Slling * string, and that it's less than ZAP_MAXNAMELEN. 984990b4856Slling */ 985990b4856Slling if (nvpair_type(elem) != DATA_TYPE_STRING) { 986990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 987990b4856Slling "'%s' must be a string"), propname); 988990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 989990b4856Slling goto error; 990990b4856Slling } 991990b4856Slling 992990b4856Slling if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) { 993990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 994990b4856Slling "property name '%s' is too long"), 995990b4856Slling propname); 996990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 997990b4856Slling goto error; 998fa9e4066Sahrens } 999fa9e4066Sahrens 1000e9dbad6fSeschrock (void) nvpair_value_string(elem, &strval); 1001e9dbad6fSeschrock if (nvlist_add_string(ret, propname, strval) != 0) { 1002e9dbad6fSeschrock (void) no_memory(hdl); 1003e9dbad6fSeschrock goto error; 1004e9dbad6fSeschrock } 1005e9dbad6fSeschrock continue; 1006e9dbad6fSeschrock } 1007e9dbad6fSeschrock 100814843421SMatthew Ahrens /* 100914843421SMatthew Ahrens * Currently, only user properties can be modified on 101014843421SMatthew Ahrens * snapshots. 101114843421SMatthew Ahrens */ 1012bb0ade09Sahrens if (type == ZFS_TYPE_SNAPSHOT) { 1013bb0ade09Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1014bb0ade09Sahrens "this property can not be modified for snapshots")); 1015bb0ade09Sahrens (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 1016bb0ade09Sahrens goto error; 1017bb0ade09Sahrens } 1018bb0ade09Sahrens 101914843421SMatthew Ahrens if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) { 102014843421SMatthew Ahrens zfs_userquota_prop_t uqtype; 102114843421SMatthew Ahrens char newpropname[128]; 102214843421SMatthew Ahrens char domain[128]; 102314843421SMatthew Ahrens uint64_t rid; 102414843421SMatthew Ahrens uint64_t valary[3]; 102514843421SMatthew Ahrens 102614843421SMatthew Ahrens if (userquota_propname_decode(propname, zoned, 102714843421SMatthew Ahrens &uqtype, domain, sizeof (domain), &rid) != 0) { 102814843421SMatthew Ahrens zfs_error_aux(hdl, 102914843421SMatthew Ahrens dgettext(TEXT_DOMAIN, 103014843421SMatthew Ahrens "'%s' has an invalid user/group name"), 103114843421SMatthew Ahrens propname); 103214843421SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 103314843421SMatthew Ahrens goto error; 103414843421SMatthew Ahrens } 103514843421SMatthew Ahrens 103614843421SMatthew Ahrens if (uqtype != ZFS_PROP_USERQUOTA && 103714843421SMatthew Ahrens uqtype != ZFS_PROP_GROUPQUOTA) { 103814843421SMatthew Ahrens zfs_error_aux(hdl, 103914843421SMatthew Ahrens dgettext(TEXT_DOMAIN, "'%s' is readonly"), 104014843421SMatthew Ahrens propname); 104114843421SMatthew Ahrens (void) zfs_error(hdl, EZFS_PROPREADONLY, 104214843421SMatthew Ahrens errbuf); 104314843421SMatthew Ahrens goto error; 104414843421SMatthew Ahrens } 104514843421SMatthew Ahrens 104614843421SMatthew Ahrens if (nvpair_type(elem) == DATA_TYPE_STRING) { 104714843421SMatthew Ahrens (void) nvpair_value_string(elem, &strval); 104814843421SMatthew Ahrens if (strcmp(strval, "none") == 0) { 104914843421SMatthew Ahrens intval = 0; 105014843421SMatthew Ahrens } else if (zfs_nicestrtonum(hdl, 105114843421SMatthew Ahrens strval, &intval) != 0) { 105214843421SMatthew Ahrens (void) zfs_error(hdl, 105314843421SMatthew Ahrens EZFS_BADPROP, errbuf); 105414843421SMatthew Ahrens goto error; 105514843421SMatthew Ahrens } 105614843421SMatthew Ahrens } else if (nvpair_type(elem) == 105714843421SMatthew Ahrens DATA_TYPE_UINT64) { 105814843421SMatthew Ahrens (void) nvpair_value_uint64(elem, &intval); 105914843421SMatthew Ahrens if (intval == 0) { 106014843421SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 106114843421SMatthew Ahrens "use 'none' to disable " 106214843421SMatthew Ahrens "userquota/groupquota")); 106314843421SMatthew Ahrens goto error; 106414843421SMatthew Ahrens } 106514843421SMatthew Ahrens } else { 106614843421SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 106714843421SMatthew Ahrens "'%s' must be a number"), propname); 106814843421SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 106914843421SMatthew Ahrens goto error; 107014843421SMatthew Ahrens } 107114843421SMatthew Ahrens 10722d5843dbSMatthew Ahrens /* 10732d5843dbSMatthew Ahrens * Encode the prop name as 10742d5843dbSMatthew Ahrens * userquota@<hex-rid>-domain, to make it easy 10752d5843dbSMatthew Ahrens * for the kernel to decode. 10762d5843dbSMatthew Ahrens */ 107714843421SMatthew Ahrens (void) snprintf(newpropname, sizeof (newpropname), 10782d5843dbSMatthew Ahrens "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype], 10792d5843dbSMatthew Ahrens (longlong_t)rid, domain); 108014843421SMatthew Ahrens valary[0] = uqtype; 108114843421SMatthew Ahrens valary[1] = rid; 108214843421SMatthew Ahrens valary[2] = intval; 108314843421SMatthew Ahrens if (nvlist_add_uint64_array(ret, newpropname, 108414843421SMatthew Ahrens valary, 3) != 0) { 108514843421SMatthew Ahrens (void) no_memory(hdl); 108614843421SMatthew Ahrens goto error; 108714843421SMatthew Ahrens } 108814843421SMatthew Ahrens continue; 108919b94df9SMatthew Ahrens } else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) { 109019b94df9SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 109119b94df9SMatthew Ahrens "'%s' is readonly"), 109219b94df9SMatthew Ahrens propname); 109319b94df9SMatthew Ahrens (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 109419b94df9SMatthew Ahrens goto error; 109514843421SMatthew Ahrens } 109614843421SMatthew Ahrens 109714843421SMatthew Ahrens if (prop == ZPROP_INVAL) { 109814843421SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 109914843421SMatthew Ahrens "invalid property '%s'"), propname); 110014843421SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 110114843421SMatthew Ahrens goto error; 110214843421SMatthew Ahrens } 110314843421SMatthew Ahrens 1104e9dbad6fSeschrock if (!zfs_prop_valid_for_type(prop, type)) { 1105e9dbad6fSeschrock zfs_error_aux(hdl, 1106e9dbad6fSeschrock dgettext(TEXT_DOMAIN, "'%s' does not " 1107e9dbad6fSeschrock "apply to datasets of this type"), propname); 1108e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 1109e9dbad6fSeschrock goto error; 1110e9dbad6fSeschrock } 1111e9dbad6fSeschrock 1112e9dbad6fSeschrock if (zfs_prop_readonly(prop) && 1113da6c28aaSamw (!zfs_prop_setonce(prop) || zhp != NULL)) { 1114e9dbad6fSeschrock zfs_error_aux(hdl, 1115e9dbad6fSeschrock dgettext(TEXT_DOMAIN, "'%s' is readonly"), 1116e9dbad6fSeschrock propname); 1117e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 1118e9dbad6fSeschrock goto error; 1119e9dbad6fSeschrock } 1120e9dbad6fSeschrock 1121990b4856Slling if (zprop_parse_value(hdl, elem, prop, type, ret, 1122990b4856Slling &strval, &intval, errbuf) != 0) 1123e9dbad6fSeschrock goto error; 1124fa9e4066Sahrens 1125e9dbad6fSeschrock /* 1126e9dbad6fSeschrock * Perform some additional checks for specific properties. 1127e9dbad6fSeschrock */ 1128e9dbad6fSeschrock switch (prop) { 1129e7437265Sahrens case ZFS_PROP_VERSION: 1130e7437265Sahrens { 1131e7437265Sahrens int version; 1132e7437265Sahrens 1133e7437265Sahrens if (zhp == NULL) 1134e7437265Sahrens break; 1135e7437265Sahrens version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION); 1136e7437265Sahrens if (intval < version) { 1137e7437265Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1138e7437265Sahrens "Can not downgrade; already at version %u"), 1139e7437265Sahrens version); 1140e7437265Sahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1141e7437265Sahrens goto error; 1142e7437265Sahrens } 1143e7437265Sahrens break; 1144e7437265Sahrens } 1145e7437265Sahrens 1146e9dbad6fSeschrock case ZFS_PROP_VOLBLOCKSIZE: 1147b5152584SMatthew Ahrens case ZFS_PROP_RECORDSIZE: 1148b5152584SMatthew Ahrens { 1149b5152584SMatthew Ahrens int maxbs = SPA_MAXBLOCKSIZE; 1150e9316f76SJoe Stein if (zpool_hdl != NULL) { 1151e9316f76SJoe Stein maxbs = zpool_get_prop_int(zpool_hdl, 1152b5152584SMatthew Ahrens ZPOOL_PROP_MAXBLOCKSIZE, NULL); 1153b5152584SMatthew Ahrens } 1154b5152584SMatthew Ahrens /* 1155b5152584SMatthew Ahrens * Volumes are limited to a volblocksize of 128KB, 1156b5152584SMatthew Ahrens * because they typically service workloads with 1157b5152584SMatthew Ahrens * small random writes, which incur a large performance 1158b5152584SMatthew Ahrens * penalty with large blocks. 1159b5152584SMatthew Ahrens */ 1160b5152584SMatthew Ahrens if (prop == ZFS_PROP_VOLBLOCKSIZE) 1161b5152584SMatthew Ahrens maxbs = SPA_OLD_MAXBLOCKSIZE; 1162b5152584SMatthew Ahrens /* 1163b5152584SMatthew Ahrens * The value must be a power of two between 1164b5152584SMatthew Ahrens * SPA_MINBLOCKSIZE and maxbs. 1165b5152584SMatthew Ahrens */ 1166e9dbad6fSeschrock if (intval < SPA_MINBLOCKSIZE || 1167b5152584SMatthew Ahrens intval > maxbs || !ISP2(intval)) { 116899653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1169b5152584SMatthew Ahrens "'%s' must be power of 2 from 512B " 1170b5152584SMatthew Ahrens "to %uKB"), propname, maxbs >> 10); 1171e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1172e9dbad6fSeschrock goto error; 1173fa9e4066Sahrens } 1174fa9e4066Sahrens break; 1175b5152584SMatthew Ahrens } 11764201a95eSRic Aleshire case ZFS_PROP_MLSLABEL: 11774201a95eSRic Aleshire { 11784201a95eSRic Aleshire /* 11794201a95eSRic Aleshire * Verify the mlslabel string and convert to 11804201a95eSRic Aleshire * internal hex label string. 11814201a95eSRic Aleshire */ 11824201a95eSRic Aleshire 11834201a95eSRic Aleshire m_label_t *new_sl; 11844201a95eSRic Aleshire char *hex = NULL; /* internal label string */ 11854201a95eSRic Aleshire 11864201a95eSRic Aleshire /* Default value is already OK. */ 11874201a95eSRic Aleshire if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0) 11884201a95eSRic Aleshire break; 11894201a95eSRic Aleshire 11904201a95eSRic Aleshire /* Verify the label can be converted to binary form */ 11914201a95eSRic Aleshire if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) || 11924201a95eSRic Aleshire (str_to_label(strval, &new_sl, MAC_LABEL, 11934201a95eSRic Aleshire L_NO_CORRECTION, NULL) == -1)) { 11944201a95eSRic Aleshire goto badlabel; 11954201a95eSRic Aleshire } 11964201a95eSRic Aleshire 11974201a95eSRic Aleshire /* Now translate to hex internal label string */ 11984201a95eSRic Aleshire if (label_to_str(new_sl, &hex, M_INTERNAL, 11994201a95eSRic Aleshire DEF_NAMES) != 0) { 12004201a95eSRic Aleshire if (hex) 12014201a95eSRic Aleshire free(hex); 12024201a95eSRic Aleshire goto badlabel; 12034201a95eSRic Aleshire } 12044201a95eSRic Aleshire m_label_free(new_sl); 12054201a95eSRic Aleshire 12064201a95eSRic Aleshire /* If string is already in internal form, we're done. */ 12074201a95eSRic Aleshire if (strcmp(strval, hex) == 0) { 12084201a95eSRic Aleshire free(hex); 12094201a95eSRic Aleshire break; 12104201a95eSRic Aleshire } 12114201a95eSRic Aleshire 12124201a95eSRic Aleshire /* Replace the label string with the internal form. */ 1213569038c9SRic Aleshire (void) nvlist_remove(ret, zfs_prop_to_name(prop), 12144201a95eSRic Aleshire DATA_TYPE_STRING); 12154201a95eSRic Aleshire verify(nvlist_add_string(ret, zfs_prop_to_name(prop), 12164201a95eSRic Aleshire hex) == 0); 12174201a95eSRic Aleshire free(hex); 12184201a95eSRic Aleshire 12194201a95eSRic Aleshire break; 12204201a95eSRic Aleshire 12214201a95eSRic Aleshire badlabel: 12224201a95eSRic Aleshire zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 12234201a95eSRic Aleshire "invalid mlslabel '%s'"), strval); 12244201a95eSRic Aleshire (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 12254201a95eSRic Aleshire m_label_free(new_sl); /* OK if null */ 12264201a95eSRic Aleshire goto error; 12274201a95eSRic Aleshire 12284201a95eSRic Aleshire } 12294201a95eSRic Aleshire 1230e9dbad6fSeschrock case ZFS_PROP_MOUNTPOINT: 123189eef05eSrm { 123289eef05eSrm namecheck_err_t why; 123389eef05eSrm 1234e9dbad6fSeschrock if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 || 1235e9dbad6fSeschrock strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0) 1236e9dbad6fSeschrock break; 1237fa9e4066Sahrens 123889eef05eSrm if (mountpoint_namecheck(strval, &why)) { 123989eef05eSrm switch (why) { 124089eef05eSrm case NAME_ERR_LEADING_SLASH: 124189eef05eSrm zfs_error_aux(hdl, 124289eef05eSrm dgettext(TEXT_DOMAIN, 124389eef05eSrm "'%s' must be an absolute path, " 124489eef05eSrm "'none', or 'legacy'"), propname); 124589eef05eSrm break; 124689eef05eSrm case NAME_ERR_TOOLONG: 124789eef05eSrm zfs_error_aux(hdl, 124889eef05eSrm dgettext(TEXT_DOMAIN, 124989eef05eSrm "component of '%s' is too long"), 125089eef05eSrm propname); 125189eef05eSrm break; 125288f61deeSIgor Kozhukhov 125388f61deeSIgor Kozhukhov default: 125488f61deeSIgor Kozhukhov zfs_error_aux(hdl, 125588f61deeSIgor Kozhukhov dgettext(TEXT_DOMAIN, 125688f61deeSIgor Kozhukhov "(%d) not defined"), 125788f61deeSIgor Kozhukhov why); 125888f61deeSIgor Kozhukhov break; 125989eef05eSrm } 1260e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1261e9dbad6fSeschrock goto error; 1262fa9e4066Sahrens } 126389eef05eSrm } 126489eef05eSrm 1265f3861e1aSahl /*FALLTHRU*/ 1266fa9e4066Sahrens 1267da6c28aaSamw case ZFS_PROP_SHARESMB: 1268f3861e1aSahl case ZFS_PROP_SHARENFS: 1269f3861e1aSahl /* 1270da6c28aaSamw * For the mountpoint and sharenfs or sharesmb 1271da6c28aaSamw * properties, check if it can be set in a 1272da6c28aaSamw * global/non-global zone based on 1273f3861e1aSahl * the zoned property value: 1274f3861e1aSahl * 1275f3861e1aSahl * global zone non-global zone 1276f3861e1aSahl * -------------------------------------------------- 1277f3861e1aSahl * zoned=on mountpoint (no) mountpoint (yes) 1278f3861e1aSahl * sharenfs (no) sharenfs (no) 1279da6c28aaSamw * sharesmb (no) sharesmb (no) 1280f3861e1aSahl * 1281f3861e1aSahl * zoned=off mountpoint (yes) N/A 1282f3861e1aSahl * sharenfs (yes) 1283da6c28aaSamw * sharesmb (yes) 1284f3861e1aSahl */ 1285e9dbad6fSeschrock if (zoned) { 1286e9dbad6fSeschrock if (getzoneid() == GLOBAL_ZONEID) { 1287e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1288e9dbad6fSeschrock "'%s' cannot be set on " 1289e9dbad6fSeschrock "dataset in a non-global zone"), 1290e9dbad6fSeschrock propname); 1291e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_ZONED, 1292e9dbad6fSeschrock errbuf); 1293e9dbad6fSeschrock goto error; 1294da6c28aaSamw } else if (prop == ZFS_PROP_SHARENFS || 1295da6c28aaSamw prop == ZFS_PROP_SHARESMB) { 1296e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1297e9dbad6fSeschrock "'%s' cannot be set in " 1298e9dbad6fSeschrock "a non-global zone"), propname); 1299e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_ZONED, 1300e9dbad6fSeschrock errbuf); 1301e9dbad6fSeschrock goto error; 1302fa9e4066Sahrens } 1303e9dbad6fSeschrock } else if (getzoneid() != GLOBAL_ZONEID) { 1304e9dbad6fSeschrock /* 1305e9dbad6fSeschrock * If zoned property is 'off', this must be in 130614843421SMatthew Ahrens * a global zone. If not, something is wrong. 1307e9dbad6fSeschrock */ 1308e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1309e9dbad6fSeschrock "'%s' cannot be set while dataset " 1310e9dbad6fSeschrock "'zoned' property is set"), propname); 1311e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_ZONED, errbuf); 1312e9dbad6fSeschrock goto error; 1313fa9e4066Sahrens } 1314f3861e1aSahl 131567331909Sdougm /* 131667331909Sdougm * At this point, it is legitimate to set the 131767331909Sdougm * property. Now we want to make sure that the 131867331909Sdougm * property value is valid if it is sharenfs. 131967331909Sdougm */ 1320da6c28aaSamw if ((prop == ZFS_PROP_SHARENFS || 1321da6c28aaSamw prop == ZFS_PROP_SHARESMB) && 1322fac3008cSeschrock strcmp(strval, "on") != 0 && 1323fac3008cSeschrock strcmp(strval, "off") != 0) { 1324da6c28aaSamw zfs_share_proto_t proto; 1325da6c28aaSamw 1326da6c28aaSamw if (prop == ZFS_PROP_SHARESMB) 1327da6c28aaSamw proto = PROTO_SMB; 1328da6c28aaSamw else 1329da6c28aaSamw proto = PROTO_NFS; 133067331909Sdougm 133167331909Sdougm /* 1332da6c28aaSamw * Must be an valid sharing protocol 1333da6c28aaSamw * option string so init the libshare 1334da6c28aaSamw * in order to enable the parser and 1335da6c28aaSamw * then parse the options. We use the 1336da6c28aaSamw * control API since we don't care about 1337da6c28aaSamw * the current configuration and don't 133867331909Sdougm * want the overhead of loading it 133967331909Sdougm * until we actually do something. 134067331909Sdougm */ 134167331909Sdougm 1342fac3008cSeschrock if (zfs_init_libshare(hdl, 1343fac3008cSeschrock SA_INIT_CONTROL_API) != SA_OK) { 1344fac3008cSeschrock /* 1345fac3008cSeschrock * An error occurred so we can't do 1346fac3008cSeschrock * anything 1347fac3008cSeschrock */ 1348fac3008cSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1349fac3008cSeschrock "'%s' cannot be set: problem " 1350fac3008cSeschrock "in share initialization"), 1351fac3008cSeschrock propname); 1352fac3008cSeschrock (void) zfs_error(hdl, EZFS_BADPROP, 1353fac3008cSeschrock errbuf); 1354fac3008cSeschrock goto error; 1355fac3008cSeschrock } 135667331909Sdougm 1357da6c28aaSamw if (zfs_parse_options(strval, proto) != SA_OK) { 1358fac3008cSeschrock /* 1359fac3008cSeschrock * There was an error in parsing so 1360fac3008cSeschrock * deal with it by issuing an error 1361fac3008cSeschrock * message and leaving after 1362fac3008cSeschrock * uninitializing the the libshare 1363fac3008cSeschrock * interface. 1364fac3008cSeschrock */ 1365fac3008cSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1366fac3008cSeschrock "'%s' cannot be set to invalid " 1367fac3008cSeschrock "options"), propname); 1368fac3008cSeschrock (void) zfs_error(hdl, EZFS_BADPROP, 1369fac3008cSeschrock errbuf); 1370fac3008cSeschrock zfs_uninit_libshare(hdl); 1371fac3008cSeschrock goto error; 1372fac3008cSeschrock } 137367331909Sdougm zfs_uninit_libshare(hdl); 137467331909Sdougm } 137567331909Sdougm 1376da6c28aaSamw break; 137788f61deeSIgor Kozhukhov 1378da6c28aaSamw case ZFS_PROP_UTF8ONLY: 1379da6c28aaSamw chosen_utf = (int)intval; 1380da6c28aaSamw break; 138188f61deeSIgor Kozhukhov 1382da6c28aaSamw case ZFS_PROP_NORMALIZE: 1383da6c28aaSamw chosen_normal = (int)intval; 1384f3861e1aSahl break; 138588f61deeSIgor Kozhukhov 138688f61deeSIgor Kozhukhov default: 138788f61deeSIgor Kozhukhov break; 1388e9dbad6fSeschrock } 1389fa9e4066Sahrens 1390e9dbad6fSeschrock /* 1391e9dbad6fSeschrock * For changes to existing volumes, we have some additional 1392e9dbad6fSeschrock * checks to enforce. 1393e9dbad6fSeschrock */ 1394e9dbad6fSeschrock if (type == ZFS_TYPE_VOLUME && zhp != NULL) { 1395e9dbad6fSeschrock uint64_t volsize = zfs_prop_get_int(zhp, 1396e9dbad6fSeschrock ZFS_PROP_VOLSIZE); 1397e9dbad6fSeschrock uint64_t blocksize = zfs_prop_get_int(zhp, 1398e9dbad6fSeschrock ZFS_PROP_VOLBLOCKSIZE); 1399e9dbad6fSeschrock char buf[64]; 1400e9dbad6fSeschrock 1401e9dbad6fSeschrock switch (prop) { 1402e9dbad6fSeschrock case ZFS_PROP_RESERVATION: 1403e9dbad6fSeschrock if (intval > volsize) { 1404e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1405e9dbad6fSeschrock "'%s' is greater than current " 1406e9dbad6fSeschrock "volume size"), propname); 1407e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_BADPROP, 1408e9dbad6fSeschrock errbuf); 1409e9dbad6fSeschrock goto error; 1410e9dbad6fSeschrock } 1411e9dbad6fSeschrock break; 1412e9dbad6fSeschrock 14131c10ae76SMike Gerdts case ZFS_PROP_REFRESERVATION: 14141c10ae76SMike Gerdts if (intval > volsize && intval != UINT64_MAX) { 14151c10ae76SMike Gerdts zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 14161c10ae76SMike Gerdts "'%s' is greater than current " 14171c10ae76SMike Gerdts "volume size"), propname); 14181c10ae76SMike Gerdts (void) zfs_error(hdl, EZFS_BADPROP, 14191c10ae76SMike Gerdts errbuf); 14201c10ae76SMike Gerdts goto error; 14211c10ae76SMike Gerdts } 14221c10ae76SMike Gerdts break; 14231c10ae76SMike Gerdts 1424e9dbad6fSeschrock case ZFS_PROP_VOLSIZE: 1425e9dbad6fSeschrock if (intval % blocksize != 0) { 1426e9dbad6fSeschrock zfs_nicenum(blocksize, buf, 1427e9dbad6fSeschrock sizeof (buf)); 1428e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1429e9dbad6fSeschrock "'%s' must be a multiple of " 1430e9dbad6fSeschrock "volume block size (%s)"), 1431e9dbad6fSeschrock propname, buf); 1432e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_BADPROP, 1433e9dbad6fSeschrock errbuf); 1434e9dbad6fSeschrock goto error; 1435e9dbad6fSeschrock } 1436e9dbad6fSeschrock 1437e9dbad6fSeschrock if (intval == 0) { 1438e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1439e9dbad6fSeschrock "'%s' cannot be zero"), 1440e9dbad6fSeschrock propname); 1441e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_BADPROP, 1442e9dbad6fSeschrock errbuf); 1443e9dbad6fSeschrock goto error; 1444e9dbad6fSeschrock } 1445f3861e1aSahl break; 144688f61deeSIgor Kozhukhov 144788f61deeSIgor Kozhukhov default: 144888f61deeSIgor Kozhukhov break; 1449fa9e4066Sahrens } 1450e9dbad6fSeschrock } 1451e9dbad6fSeschrock } 1452fa9e4066Sahrens 1453da6c28aaSamw /* 1454da6c28aaSamw * If normalization was chosen, but no UTF8 choice was made, 1455da6c28aaSamw * enforce rejection of non-UTF8 names. 1456da6c28aaSamw * 1457da6c28aaSamw * If normalization was chosen, but rejecting non-UTF8 names 1458da6c28aaSamw * was explicitly not chosen, it is an error. 1459da6c28aaSamw */ 1460de8267e0Stimh if (chosen_normal > 0 && chosen_utf < 0) { 1461da6c28aaSamw if (nvlist_add_uint64(ret, 1462da6c28aaSamw zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) { 1463da6c28aaSamw (void) no_memory(hdl); 1464da6c28aaSamw goto error; 1465da6c28aaSamw } 1466de8267e0Stimh } else if (chosen_normal > 0 && chosen_utf == 0) { 1467da6c28aaSamw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1468da6c28aaSamw "'%s' must be set 'on' if normalization chosen"), 1469da6c28aaSamw zfs_prop_to_name(ZFS_PROP_UTF8ONLY)); 1470da6c28aaSamw (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1471da6c28aaSamw goto error; 1472da6c28aaSamw } 147336db6475SEric Taylor return (ret); 147436db6475SEric Taylor 147536db6475SEric Taylor error: 147636db6475SEric Taylor nvlist_free(ret); 147736db6475SEric Taylor return (NULL); 147836db6475SEric Taylor } 147936db6475SEric Taylor 148036db6475SEric Taylor int 148136db6475SEric Taylor zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl) 148236db6475SEric Taylor { 148336db6475SEric Taylor uint64_t old_volsize; 148436db6475SEric Taylor uint64_t new_volsize; 148536db6475SEric Taylor uint64_t old_reservation; 148636db6475SEric Taylor uint64_t new_reservation; 148736db6475SEric Taylor zfs_prop_t resv_prop; 1488c61ea566SGeorge Wilson nvlist_t *props; 1489da6c28aaSamw 1490e9dbad6fSeschrock /* 1491e9dbad6fSeschrock * If this is an existing volume, and someone is setting the volsize, 1492e9dbad6fSeschrock * make sure that it matches the reservation, or add it if necessary. 1493e9dbad6fSeschrock */ 149436db6475SEric Taylor old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 149536db6475SEric Taylor if (zfs_which_resv_prop(zhp, &resv_prop) < 0) 149636db6475SEric Taylor return (-1); 149736db6475SEric Taylor old_reservation = zfs_prop_get_int(zhp, resv_prop); 1498c61ea566SGeorge Wilson 1499c61ea566SGeorge Wilson props = fnvlist_alloc(); 1500c61ea566SGeorge Wilson fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 1501c61ea566SGeorge Wilson zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE)); 1502c61ea566SGeorge Wilson 1503c61ea566SGeorge Wilson if ((zvol_volsize_to_reservation(old_volsize, props) != 1504c61ea566SGeorge Wilson old_reservation) || nvlist_exists(nvl, 1505c61ea566SGeorge Wilson zfs_prop_to_name(resv_prop))) { 1506c61ea566SGeorge Wilson fnvlist_free(props); 150736db6475SEric Taylor return (0); 1508fa9e4066Sahrens } 150936db6475SEric Taylor if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE), 1510c61ea566SGeorge Wilson &new_volsize) != 0) { 1511c61ea566SGeorge Wilson fnvlist_free(props); 151236db6475SEric Taylor return (-1); 1513c61ea566SGeorge Wilson } 1514c61ea566SGeorge Wilson new_reservation = zvol_volsize_to_reservation(new_volsize, props); 1515c61ea566SGeorge Wilson fnvlist_free(props); 1516c61ea566SGeorge Wilson 151736db6475SEric Taylor if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop), 151836db6475SEric Taylor new_reservation) != 0) { 151936db6475SEric Taylor (void) no_memory(zhp->zfs_hdl); 152036db6475SEric Taylor return (-1); 152136db6475SEric Taylor } 152236db6475SEric Taylor return (1); 1523fa9e4066Sahrens } 1524fa9e4066Sahrens 15251c10ae76SMike Gerdts /* 15261c10ae76SMike Gerdts * Helper for 'zfs {set|clone} refreservation=auto'. Must be called after 15271c10ae76SMike Gerdts * zfs_valid_proplist(), as it is what sets the UINT64_MAX sentinal value. 15281c10ae76SMike Gerdts * Return codes must match zfs_add_synthetic_resv(). 15291c10ae76SMike Gerdts */ 15301c10ae76SMike Gerdts static int 15311c10ae76SMike Gerdts zfs_fix_auto_resv(zfs_handle_t *zhp, nvlist_t *nvl) 15321c10ae76SMike Gerdts { 15331c10ae76SMike Gerdts uint64_t volsize; 15341c10ae76SMike Gerdts uint64_t resvsize; 15351c10ae76SMike Gerdts zfs_prop_t prop; 15361c10ae76SMike Gerdts nvlist_t *props; 15371c10ae76SMike Gerdts 15381c10ae76SMike Gerdts if (!ZFS_IS_VOLUME(zhp)) { 15391c10ae76SMike Gerdts return (0); 15401c10ae76SMike Gerdts } 15411c10ae76SMike Gerdts 15421c10ae76SMike Gerdts if (zfs_which_resv_prop(zhp, &prop) != 0) { 15431c10ae76SMike Gerdts return (-1); 15441c10ae76SMike Gerdts } 15451c10ae76SMike Gerdts 15461c10ae76SMike Gerdts if (prop != ZFS_PROP_REFRESERVATION) { 15471c10ae76SMike Gerdts return (0); 15481c10ae76SMike Gerdts } 15491c10ae76SMike Gerdts 15501c10ae76SMike Gerdts if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(prop), &resvsize) != 0) { 15511c10ae76SMike Gerdts /* No value being set, so it can't be "auto" */ 15521c10ae76SMike Gerdts return (0); 15531c10ae76SMike Gerdts } 15541c10ae76SMike Gerdts if (resvsize != UINT64_MAX) { 15551c10ae76SMike Gerdts /* Being set to a value other than "auto" */ 15561c10ae76SMike Gerdts return (0); 15571c10ae76SMike Gerdts } 15581c10ae76SMike Gerdts 15591c10ae76SMike Gerdts props = fnvlist_alloc(); 15601c10ae76SMike Gerdts 15611c10ae76SMike Gerdts fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 15621c10ae76SMike Gerdts zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE)); 15631c10ae76SMike Gerdts 15641c10ae76SMike Gerdts if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE), 15651c10ae76SMike Gerdts &volsize) != 0) { 15661c10ae76SMike Gerdts volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 15671c10ae76SMike Gerdts } 15681c10ae76SMike Gerdts 15691c10ae76SMike Gerdts resvsize = zvol_volsize_to_reservation(volsize, props); 15701c10ae76SMike Gerdts fnvlist_free(props); 15711c10ae76SMike Gerdts 15721c10ae76SMike Gerdts (void) nvlist_remove_all(nvl, zfs_prop_to_name(prop)); 15731c10ae76SMike Gerdts if (nvlist_add_uint64(nvl, zfs_prop_to_name(prop), resvsize) != 0) { 15741c10ae76SMike Gerdts (void) no_memory(zhp->zfs_hdl); 15751c10ae76SMike Gerdts return (-1); 15761c10ae76SMike Gerdts } 15771c10ae76SMike Gerdts return (1); 15781c10ae76SMike Gerdts } 15791c10ae76SMike Gerdts 158092241e0bSTom Erickson void 158192241e0bSTom Erickson zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err, 158292241e0bSTom Erickson char *errbuf) 158392241e0bSTom Erickson { 158492241e0bSTom Erickson switch (err) { 158592241e0bSTom Erickson 158692241e0bSTom Erickson case ENOSPC: 158792241e0bSTom Erickson /* 158892241e0bSTom Erickson * For quotas and reservations, ENOSPC indicates 158992241e0bSTom Erickson * something different; setting a quota or reservation 159092241e0bSTom Erickson * doesn't use any disk space. 159192241e0bSTom Erickson */ 159292241e0bSTom Erickson switch (prop) { 159392241e0bSTom Erickson case ZFS_PROP_QUOTA: 159492241e0bSTom Erickson case ZFS_PROP_REFQUOTA: 159592241e0bSTom Erickson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 159692241e0bSTom Erickson "size is less than current used or " 159792241e0bSTom Erickson "reserved space")); 159892241e0bSTom Erickson (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 159992241e0bSTom Erickson break; 160092241e0bSTom Erickson 160192241e0bSTom Erickson case ZFS_PROP_RESERVATION: 160292241e0bSTom Erickson case ZFS_PROP_REFRESERVATION: 160392241e0bSTom Erickson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 160492241e0bSTom Erickson "size is greater than available space")); 160592241e0bSTom Erickson (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 160692241e0bSTom Erickson break; 160792241e0bSTom Erickson 160892241e0bSTom Erickson default: 160992241e0bSTom Erickson (void) zfs_standard_error(hdl, err, errbuf); 161092241e0bSTom Erickson break; 161192241e0bSTom Erickson } 161292241e0bSTom Erickson break; 161392241e0bSTom Erickson 161492241e0bSTom Erickson case EBUSY: 161592241e0bSTom Erickson (void) zfs_standard_error(hdl, EBUSY, errbuf); 161692241e0bSTom Erickson break; 161792241e0bSTom Erickson 161892241e0bSTom Erickson case EROFS: 161992241e0bSTom Erickson (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf); 162092241e0bSTom Erickson break; 162192241e0bSTom Erickson 16226fdcb3d1SWill Andrews case E2BIG: 16236fdcb3d1SWill Andrews zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 16246fdcb3d1SWill Andrews "property value too long")); 16256fdcb3d1SWill Andrews (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 16266fdcb3d1SWill Andrews break; 16276fdcb3d1SWill Andrews 162892241e0bSTom Erickson case ENOTSUP: 162992241e0bSTom Erickson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 163092241e0bSTom Erickson "pool and or dataset must be upgraded to set this " 163192241e0bSTom Erickson "property or value")); 163292241e0bSTom Erickson (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 163392241e0bSTom Erickson break; 163492241e0bSTom Erickson 163592241e0bSTom Erickson case ERANGE: 1636b5152584SMatthew Ahrens if (prop == ZFS_PROP_COMPRESSION || 1637b5152584SMatthew Ahrens prop == ZFS_PROP_RECORDSIZE) { 163892241e0bSTom Erickson (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 163992241e0bSTom Erickson "property setting is not allowed on " 164092241e0bSTom Erickson "bootable datasets")); 164192241e0bSTom Erickson (void) zfs_error(hdl, EZFS_NOTSUP, errbuf); 164245818ee1SMatthew Ahrens } else if (prop == ZFS_PROP_CHECKSUM || 164345818ee1SMatthew Ahrens prop == ZFS_PROP_DEDUP) { 164445818ee1SMatthew Ahrens (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 164545818ee1SMatthew Ahrens "property setting is not allowed on " 164645818ee1SMatthew Ahrens "root pools")); 164745818ee1SMatthew Ahrens (void) zfs_error(hdl, EZFS_NOTSUP, errbuf); 164892241e0bSTom Erickson } else { 164992241e0bSTom Erickson (void) zfs_standard_error(hdl, err, errbuf); 165092241e0bSTom Erickson } 165192241e0bSTom Erickson break; 165292241e0bSTom Erickson 1653ab003da8SJim Dunham case EINVAL: 1654ab003da8SJim Dunham if (prop == ZPROP_INVAL) { 1655ab003da8SJim Dunham (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1656ab003da8SJim Dunham } else { 1657ab003da8SJim Dunham (void) zfs_standard_error(hdl, err, errbuf); 1658ab003da8SJim Dunham } 1659ab003da8SJim Dunham break; 1660ab003da8SJim Dunham 166192241e0bSTom Erickson case EOVERFLOW: 166292241e0bSTom Erickson /* 166392241e0bSTom Erickson * This platform can't address a volume this big. 166492241e0bSTom Erickson */ 166592241e0bSTom Erickson #ifdef _ILP32 166692241e0bSTom Erickson if (prop == ZFS_PROP_VOLSIZE) { 166792241e0bSTom Erickson (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf); 166892241e0bSTom Erickson break; 166992241e0bSTom Erickson } 167092241e0bSTom Erickson #endif 167192241e0bSTom Erickson /* FALLTHROUGH */ 167292241e0bSTom Erickson default: 167392241e0bSTom Erickson (void) zfs_standard_error(hdl, err, errbuf); 167492241e0bSTom Erickson } 167592241e0bSTom Erickson } 167692241e0bSTom Erickson 1677fa9e4066Sahrens /* 1678fa9e4066Sahrens * Given a property name and value, set the property for the given dataset. 1679fa9e4066Sahrens */ 1680fa9e4066Sahrens int 1681e9dbad6fSeschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval) 1682fa9e4066Sahrens { 1683e9dbad6fSeschrock int ret = -1; 168499653d4eSeschrock char errbuf[1024]; 168599653d4eSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 168630925561SChris Williamson nvlist_t *nvl = NULL; 168799653d4eSeschrock 168899653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), 1689e9dbad6fSeschrock dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 169099653d4eSeschrock zhp->zfs_name); 169199653d4eSeschrock 1692e9dbad6fSeschrock if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 || 1693e9dbad6fSeschrock nvlist_add_string(nvl, propname, propval) != 0) { 1694e9dbad6fSeschrock (void) no_memory(hdl); 1695e9dbad6fSeschrock goto error; 1696fa9e4066Sahrens } 1697fa9e4066Sahrens 169830925561SChris Williamson ret = zfs_prop_set_list(zhp, nvl); 1699990b4856Slling 170030925561SChris Williamson error: 1701e9dbad6fSeschrock nvlist_free(nvl); 170230925561SChris Williamson return (ret); 170330925561SChris Williamson } 1704e9dbad6fSeschrock 1705e9dbad6fSeschrock 170636db6475SEric Taylor 170730925561SChris Williamson /* 170830925561SChris Williamson * Given an nvlist of property names and values, set the properties for the 170930925561SChris Williamson * given dataset. 171030925561SChris Williamson */ 171130925561SChris Williamson int 171230925561SChris Williamson zfs_prop_set_list(zfs_handle_t *zhp, nvlist_t *props) 171330925561SChris Williamson { 171430925561SChris Williamson zfs_cmd_t zc = { 0 }; 171530925561SChris Williamson int ret = -1; 171630925561SChris Williamson prop_changelist_t **cls = NULL; 171730925561SChris Williamson int cl_idx; 171830925561SChris Williamson char errbuf[1024]; 171930925561SChris Williamson libzfs_handle_t *hdl = zhp->zfs_hdl; 172030925561SChris Williamson nvlist_t *nvl; 172130925561SChris Williamson int nvl_len; 1722f83b46baSPaul Dagnelie int added_resv = 0; 1723fa9e4066Sahrens 172430925561SChris Williamson (void) snprintf(errbuf, sizeof (errbuf), 172530925561SChris Williamson dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 172630925561SChris Williamson zhp->zfs_name); 172730925561SChris Williamson 172830925561SChris Williamson if ((nvl = zfs_valid_proplist(hdl, zhp->zfs_type, props, 1729e9316f76SJoe Stein zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, zhp->zpool_hdl, 1730e9316f76SJoe Stein errbuf)) == NULL) 1731fa9e4066Sahrens goto error; 1732fa9e4066Sahrens 17330068372bSMark J Musante /* 173430925561SChris Williamson * We have to check for any extra properties which need to be added 173530925561SChris Williamson * before computing the length of the nvlist. 17360068372bSMark J Musante */ 173730925561SChris Williamson for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL); 173830925561SChris Williamson elem != NULL; 173930925561SChris Williamson elem = nvlist_next_nvpair(nvl, elem)) { 174030925561SChris Williamson if (zfs_name_to_prop(nvpair_name(elem)) == ZFS_PROP_VOLSIZE && 174130925561SChris Williamson (added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1) { 174230925561SChris Williamson goto error; 174330925561SChris Williamson } 17444445fffbSMatthew Ahrens } 17451c10ae76SMike Gerdts 17461c10ae76SMike Gerdts if (added_resv != 1 && 17471c10ae76SMike Gerdts (added_resv = zfs_fix_auto_resv(zhp, nvl)) == -1) { 17481c10ae76SMike Gerdts goto error; 17491c10ae76SMike Gerdts } 17501c10ae76SMike Gerdts 175130925561SChris Williamson /* 175230925561SChris Williamson * Check how many properties we're setting and allocate an array to 175330925561SChris Williamson * store changelist pointers for postfix(). 175430925561SChris Williamson */ 175530925561SChris Williamson nvl_len = 0; 175630925561SChris Williamson for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL); 175730925561SChris Williamson elem != NULL; 175830925561SChris Williamson elem = nvlist_next_nvpair(nvl, elem)) 175930925561SChris Williamson nvl_len++; 176030925561SChris Williamson if ((cls = calloc(nvl_len, sizeof (prop_changelist_t *))) == NULL) 17610068372bSMark J Musante goto error; 1762fa9e4066Sahrens 176330925561SChris Williamson cl_idx = 0; 176430925561SChris Williamson for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL); 176530925561SChris Williamson elem != NULL; 176630925561SChris Williamson elem = nvlist_next_nvpair(nvl, elem)) { 176730925561SChris Williamson 176830925561SChris Williamson zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem)); 176930925561SChris Williamson 177030925561SChris Williamson assert(cl_idx < nvl_len); 177130925561SChris Williamson /* 177230925561SChris Williamson * We don't want to unmount & remount the dataset when changing 177330925561SChris Williamson * its canmount property to 'on' or 'noauto'. We only use 177430925561SChris Williamson * the changelist logic to unmount when setting canmount=off. 177530925561SChris Williamson */ 1776c079fa4dSAndriy Gapon if (prop != ZFS_PROP_CANMOUNT || 1777c079fa4dSAndriy Gapon (fnvpair_value_uint64(elem) == ZFS_CANMOUNT_OFF && 1778c079fa4dSAndriy Gapon zfs_is_mounted(zhp, NULL))) { 177930925561SChris Williamson cls[cl_idx] = changelist_gather(zhp, prop, 0, 0); 178030925561SChris Williamson if (cls[cl_idx] == NULL) 178130925561SChris Williamson goto error; 178230925561SChris Williamson } 178330925561SChris Williamson 178430925561SChris Williamson if (prop == ZFS_PROP_MOUNTPOINT && 178530925561SChris Williamson changelist_haszonedchild(cls[cl_idx])) { 178630925561SChris Williamson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 178730925561SChris Williamson "child dataset with inherited mountpoint is used " 178830925561SChris Williamson "in a non-global zone")); 178930925561SChris Williamson ret = zfs_error(hdl, EZFS_ZONED, errbuf); 179030925561SChris Williamson goto error; 179130925561SChris Williamson } 179230925561SChris Williamson 179330925561SChris Williamson if (cls[cl_idx] != NULL && 179430925561SChris Williamson (ret = changelist_prefix(cls[cl_idx])) != 0) 179530925561SChris Williamson goto error; 179630925561SChris Williamson 179730925561SChris Williamson cl_idx++; 179830925561SChris Williamson } 179930925561SChris Williamson assert(cl_idx == nvl_len); 180030925561SChris Williamson 1801fa9e4066Sahrens /* 180230925561SChris Williamson * Execute the corresponding ioctl() to set this list of properties. 1803fa9e4066Sahrens */ 1804fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1805fa9e4066Sahrens 180630925561SChris Williamson if ((ret = zcmd_write_src_nvlist(hdl, &zc, nvl)) != 0 || 180730925561SChris Williamson (ret = zcmd_alloc_dst_nvlist(hdl, &zc, 0)) != 0) 1808e9dbad6fSeschrock goto error; 1809e9dbad6fSeschrock 1810ecd6cf80Smarks ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc); 1811743a77edSAlan Wright 1812fa9e4066Sahrens if (ret != 0) { 1813f62db44dSAndrew Stormont if (zc.zc_nvlist_dst_filled == B_FALSE) { 1814f62db44dSAndrew Stormont (void) zfs_standard_error(hdl, errno, errbuf); 1815f62db44dSAndrew Stormont goto error; 1816f62db44dSAndrew Stormont } 1817f62db44dSAndrew Stormont 181830925561SChris Williamson /* Get the list of unset properties back and report them. */ 181930925561SChris Williamson nvlist_t *errorprops = NULL; 182030925561SChris Williamson if (zcmd_read_dst_nvlist(hdl, &zc, &errorprops) != 0) 182130925561SChris Williamson goto error; 1822f62db44dSAndrew Stormont for (nvpair_t *elem = nvlist_next_nvpair(errorprops, NULL); 182330925561SChris Williamson elem != NULL; 1824f62db44dSAndrew Stormont elem = nvlist_next_nvpair(errorprops, elem)) { 182530925561SChris Williamson zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem)); 182630925561SChris Williamson zfs_setprop_error(hdl, prop, errno, errbuf); 182730925561SChris Williamson } 182830925561SChris Williamson nvlist_free(errorprops); 182930925561SChris Williamson 183036db6475SEric Taylor if (added_resv && errno == ENOSPC) { 183136db6475SEric Taylor /* clean up the volsize property we tried to set */ 183236db6475SEric Taylor uint64_t old_volsize = zfs_prop_get_int(zhp, 183336db6475SEric Taylor ZFS_PROP_VOLSIZE); 183436db6475SEric Taylor nvlist_free(nvl); 183530925561SChris Williamson nvl = NULL; 183636db6475SEric Taylor zcmd_free_nvlists(&zc); 183730925561SChris Williamson 183836db6475SEric Taylor if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) 183936db6475SEric Taylor goto error; 184036db6475SEric Taylor if (nvlist_add_uint64(nvl, 184136db6475SEric Taylor zfs_prop_to_name(ZFS_PROP_VOLSIZE), 184236db6475SEric Taylor old_volsize) != 0) 184336db6475SEric Taylor goto error; 184436db6475SEric Taylor if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0) 184536db6475SEric Taylor goto error; 184636db6475SEric Taylor (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc); 184736db6475SEric Taylor } 1848fa9e4066Sahrens } else { 184930925561SChris Williamson for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) { 185030925561SChris Williamson if (cls[cl_idx] != NULL) { 185130925561SChris Williamson int clp_err = changelist_postfix(cls[cl_idx]); 185230925561SChris Williamson if (clp_err != 0) 185330925561SChris Williamson ret = clp_err; 185430925561SChris Williamson } 185530925561SChris Williamson } 1856a227b7f4Shs 1857fa9e4066Sahrens /* 1858fa9e4066Sahrens * Refresh the statistics so the new property value 1859fa9e4066Sahrens * is reflected. 1860fa9e4066Sahrens */ 1861a227b7f4Shs if (ret == 0) 1862e9dbad6fSeschrock (void) get_stats(zhp); 1863fa9e4066Sahrens } 1864fa9e4066Sahrens 1865fa9e4066Sahrens error: 1866e9dbad6fSeschrock nvlist_free(nvl); 1867e9dbad6fSeschrock zcmd_free_nvlists(&zc); 186830925561SChris Williamson if (cls != NULL) { 186930925561SChris Williamson for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) { 187030925561SChris Williamson if (cls[cl_idx] != NULL) 187130925561SChris Williamson changelist_free(cls[cl_idx]); 187230925561SChris Williamson } 187330925561SChris Williamson free(cls); 187430925561SChris Williamson } 1875fa9e4066Sahrens return (ret); 1876fa9e4066Sahrens } 1877fa9e4066Sahrens 1878fa9e4066Sahrens /* 187992241e0bSTom Erickson * Given a property, inherit the value from the parent dataset, or if received 188092241e0bSTom Erickson * is TRUE, revert to the received value, if any. 1881fa9e4066Sahrens */ 1882fa9e4066Sahrens int 188392241e0bSTom Erickson zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received) 1884fa9e4066Sahrens { 1885fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 1886fa9e4066Sahrens int ret; 1887fa9e4066Sahrens prop_changelist_t *cl; 188899653d4eSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 188999653d4eSeschrock char errbuf[1024]; 1890e9dbad6fSeschrock zfs_prop_t prop; 189199653d4eSeschrock 189299653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 189399653d4eSeschrock "cannot inherit %s for '%s'"), propname, zhp->zfs_name); 1894fa9e4066Sahrens 189592241e0bSTom Erickson zc.zc_cookie = received; 1896990b4856Slling if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) { 1897e9dbad6fSeschrock /* 1898e9dbad6fSeschrock * For user properties, the amount of work we have to do is very 1899e9dbad6fSeschrock * small, so just do it here. 1900e9dbad6fSeschrock */ 1901e9dbad6fSeschrock if (!zfs_prop_user(propname)) { 1902e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1903e9dbad6fSeschrock "invalid property")); 1904e9dbad6fSeschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 1905e9dbad6fSeschrock } 1906e9dbad6fSeschrock 1907e9dbad6fSeschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1908e9dbad6fSeschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 1909e9dbad6fSeschrock 1910e45ce728Sahrens if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0) 1911e9dbad6fSeschrock return (zfs_standard_error(hdl, errno, errbuf)); 1912e9dbad6fSeschrock 1913e9dbad6fSeschrock return (0); 1914e9dbad6fSeschrock } 1915e9dbad6fSeschrock 1916fa9e4066Sahrens /* 1917fa9e4066Sahrens * Verify that this property is inheritable. 1918fa9e4066Sahrens */ 191999653d4eSeschrock if (zfs_prop_readonly(prop)) 192099653d4eSeschrock return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf)); 1921fa9e4066Sahrens 192292241e0bSTom Erickson if (!zfs_prop_inheritable(prop) && !received) 192399653d4eSeschrock return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf)); 1924fa9e4066Sahrens 1925fa9e4066Sahrens /* 1926fa9e4066Sahrens * Check to see if the value applies to this type 1927fa9e4066Sahrens */ 192899653d4eSeschrock if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 192999653d4eSeschrock return (zfs_error(hdl, EZFS_PROPTYPE, errbuf)); 1930fa9e4066Sahrens 1931bf7c2d40Srm /* 193236db6475SEric Taylor * Normalize the name, to get rid of shorthand abbreviations. 1933bf7c2d40Srm */ 1934bf7c2d40Srm propname = zfs_prop_to_name(prop); 1935fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1936e9dbad6fSeschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 1937fa9e4066Sahrens 1938fa9e4066Sahrens if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID && 1939fa9e4066Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 194099653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 194199653d4eSeschrock "dataset is used in a non-global zone")); 194299653d4eSeschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 1943fa9e4066Sahrens } 1944fa9e4066Sahrens 1945fa9e4066Sahrens /* 1946fa9e4066Sahrens * Determine datasets which will be affected by this change, if any. 1947fa9e4066Sahrens */ 19480069fd67STim Haley if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL) 1949fa9e4066Sahrens return (-1); 1950fa9e4066Sahrens 1951fa9e4066Sahrens if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 195299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 195399653d4eSeschrock "child dataset with inherited mountpoint is used " 195499653d4eSeschrock "in a non-global zone")); 195599653d4eSeschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1956fa9e4066Sahrens goto error; 1957fa9e4066Sahrens } 1958fa9e4066Sahrens 1959fa9e4066Sahrens if ((ret = changelist_prefix(cl)) != 0) 1960fa9e4066Sahrens goto error; 1961fa9e4066Sahrens 1962e45ce728Sahrens if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) { 196399653d4eSeschrock return (zfs_standard_error(hdl, errno, errbuf)); 1964fa9e4066Sahrens } else { 1965fa9e4066Sahrens 1966efc555ebSnd if ((ret = changelist_postfix(cl)) != 0) 1967fa9e4066Sahrens goto error; 1968fa9e4066Sahrens 1969fa9e4066Sahrens /* 1970fa9e4066Sahrens * Refresh the statistics so the new property is reflected. 1971fa9e4066Sahrens */ 1972fa9e4066Sahrens (void) get_stats(zhp); 1973fa9e4066Sahrens } 1974fa9e4066Sahrens 1975fa9e4066Sahrens error: 1976fa9e4066Sahrens changelist_free(cl); 1977fa9e4066Sahrens return (ret); 1978fa9e4066Sahrens } 1979fa9e4066Sahrens 19807f7322feSeschrock /* 19817f7322feSeschrock * True DSL properties are stored in an nvlist. The following two functions 19827f7322feSeschrock * extract them appropriately. 19837f7322feSeschrock */ 19847f7322feSeschrock static uint64_t 19857f7322feSeschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 19867f7322feSeschrock { 19877f7322feSeschrock nvlist_t *nv; 19887f7322feSeschrock uint64_t value; 19897f7322feSeschrock 1990a2eea2e1Sahrens *source = NULL; 19917f7322feSeschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 19927f7322feSeschrock zfs_prop_to_name(prop), &nv) == 0) { 1993990b4856Slling verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0); 1994990b4856Slling (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 19957f7322feSeschrock } else { 19962e5e9e19SSanjeev Bagewadi verify(!zhp->zfs_props_table || 19972e5e9e19SSanjeev Bagewadi zhp->zfs_props_table[prop] == B_TRUE); 19987f7322feSeschrock value = zfs_prop_default_numeric(prop); 19997f7322feSeschrock *source = ""; 20007f7322feSeschrock } 20017f7322feSeschrock 20027f7322feSeschrock return (value); 20037f7322feSeschrock } 20047f7322feSeschrock 20059c3fd121SMatthew Ahrens static const char * 20067f7322feSeschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 20077f7322feSeschrock { 20087f7322feSeschrock nvlist_t *nv; 20099c3fd121SMatthew Ahrens const char *value; 20107f7322feSeschrock 2011a2eea2e1Sahrens *source = NULL; 20127f7322feSeschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 20137f7322feSeschrock zfs_prop_to_name(prop), &nv) == 0) { 20149c3fd121SMatthew Ahrens value = fnvlist_lookup_string(nv, ZPROP_VALUE); 2015990b4856Slling (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 20167f7322feSeschrock } else { 20172e5e9e19SSanjeev Bagewadi verify(!zhp->zfs_props_table || 20182e5e9e19SSanjeev Bagewadi zhp->zfs_props_table[prop] == B_TRUE); 20199c3fd121SMatthew Ahrens value = zfs_prop_default_string(prop); 20207f7322feSeschrock *source = ""; 20217f7322feSeschrock } 20227f7322feSeschrock 20237f7322feSeschrock return (value); 20247f7322feSeschrock } 20257f7322feSeschrock 202692241e0bSTom Erickson static boolean_t 202792241e0bSTom Erickson zfs_is_recvd_props_mode(zfs_handle_t *zhp) 202892241e0bSTom Erickson { 202992241e0bSTom Erickson return (zhp->zfs_props == zhp->zfs_recvd_props); 203092241e0bSTom Erickson } 203192241e0bSTom Erickson 203292241e0bSTom Erickson static void 203392241e0bSTom Erickson zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie) 203492241e0bSTom Erickson { 203592241e0bSTom Erickson *cookie = (uint64_t)(uintptr_t)zhp->zfs_props; 203692241e0bSTom Erickson zhp->zfs_props = zhp->zfs_recvd_props; 203792241e0bSTom Erickson } 203892241e0bSTom Erickson 203992241e0bSTom Erickson static void 204092241e0bSTom Erickson zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie) 204192241e0bSTom Erickson { 204292241e0bSTom Erickson zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie; 204392241e0bSTom Erickson *cookie = 0; 204492241e0bSTom Erickson } 204592241e0bSTom Erickson 2046fa9e4066Sahrens /* 2047fa9e4066Sahrens * Internal function for getting a numeric property. Both zfs_prop_get() and 2048fa9e4066Sahrens * zfs_prop_get_int() are built using this interface. 2049fa9e4066Sahrens * 2050fa9e4066Sahrens * Certain properties can be overridden using 'mount -o'. In this case, scan 2051fa9e4066Sahrens * the contents of the /etc/mnttab entry, searching for the appropriate options. 2052fa9e4066Sahrens * If they differ from the on-disk values, report the current values and mark 2053fa9e4066Sahrens * the source "temporary". 2054fa9e4066Sahrens */ 205599653d4eSeschrock static int 2056990b4856Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src, 205799653d4eSeschrock char **source, uint64_t *val) 2058fa9e4066Sahrens { 2059bd00f61bSrm zfs_cmd_t zc = { 0 }; 206096510749Stimh nvlist_t *zplprops = NULL; 2061fa9e4066Sahrens struct mnttab mnt; 20623ccfa83cSahrens char *mntopt_on = NULL; 20633ccfa83cSahrens char *mntopt_off = NULL; 206492241e0bSTom Erickson boolean_t received = zfs_is_recvd_props_mode(zhp); 2065fa9e4066Sahrens 2066fa9e4066Sahrens *source = NULL; 2067fa9e4066Sahrens 20683ccfa83cSahrens switch (prop) { 20693ccfa83cSahrens case ZFS_PROP_ATIME: 20703ccfa83cSahrens mntopt_on = MNTOPT_ATIME; 20713ccfa83cSahrens mntopt_off = MNTOPT_NOATIME; 20723ccfa83cSahrens break; 20733ccfa83cSahrens 20743ccfa83cSahrens case ZFS_PROP_DEVICES: 20753ccfa83cSahrens mntopt_on = MNTOPT_DEVICES; 20763ccfa83cSahrens mntopt_off = MNTOPT_NODEVICES; 20773ccfa83cSahrens break; 20783ccfa83cSahrens 20793ccfa83cSahrens case ZFS_PROP_EXEC: 20803ccfa83cSahrens mntopt_on = MNTOPT_EXEC; 20813ccfa83cSahrens mntopt_off = MNTOPT_NOEXEC; 20823ccfa83cSahrens break; 20833ccfa83cSahrens 20843ccfa83cSahrens case ZFS_PROP_READONLY: 20853ccfa83cSahrens mntopt_on = MNTOPT_RO; 20863ccfa83cSahrens mntopt_off = MNTOPT_RW; 20873ccfa83cSahrens break; 20883ccfa83cSahrens 20893ccfa83cSahrens case ZFS_PROP_SETUID: 20903ccfa83cSahrens mntopt_on = MNTOPT_SETUID; 20913ccfa83cSahrens mntopt_off = MNTOPT_NOSETUID; 20923ccfa83cSahrens break; 20933ccfa83cSahrens 20943ccfa83cSahrens case ZFS_PROP_XATTR: 20953ccfa83cSahrens mntopt_on = MNTOPT_XATTR; 20963ccfa83cSahrens mntopt_off = MNTOPT_NOXATTR; 20973ccfa83cSahrens break; 2098da6c28aaSamw 2099da6c28aaSamw case ZFS_PROP_NBMAND: 2100da6c28aaSamw mntopt_on = MNTOPT_NBMAND; 2101da6c28aaSamw mntopt_off = MNTOPT_NONBMAND; 2102da6c28aaSamw break; 210388f61deeSIgor Kozhukhov 210488f61deeSIgor Kozhukhov default: 210588f61deeSIgor Kozhukhov break; 21063ccfa83cSahrens } 21073ccfa83cSahrens 21083bb79becSeschrock /* 21093bb79becSeschrock * Because looking up the mount options is potentially expensive 21103bb79becSeschrock * (iterating over all of /etc/mnttab), we defer its calculation until 21113bb79becSeschrock * we're looking up a property which requires its presence. 21123bb79becSeschrock */ 21133bb79becSeschrock if (!zhp->zfs_mntcheck && 21143ccfa83cSahrens (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) { 2115ebedde84SEric Taylor libzfs_handle_t *hdl = zhp->zfs_hdl; 2116ebedde84SEric Taylor struct mnttab entry; 21173bb79becSeschrock 2118ebedde84SEric Taylor if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) { 2119ebedde84SEric Taylor zhp->zfs_mntopts = zfs_strdup(hdl, 21203ccfa83cSahrens entry.mnt_mntopts); 21213ccfa83cSahrens if (zhp->zfs_mntopts == NULL) 21223ccfa83cSahrens return (-1); 21233ccfa83cSahrens } 21243bb79becSeschrock 21253bb79becSeschrock zhp->zfs_mntcheck = B_TRUE; 21263bb79becSeschrock } 21273bb79becSeschrock 2128fa9e4066Sahrens if (zhp->zfs_mntopts == NULL) 2129fa9e4066Sahrens mnt.mnt_mntopts = ""; 2130fa9e4066Sahrens else 2131fa9e4066Sahrens mnt.mnt_mntopts = zhp->zfs_mntopts; 2132fa9e4066Sahrens 2133fa9e4066Sahrens switch (prop) { 2134fa9e4066Sahrens case ZFS_PROP_ATIME: 2135fa9e4066Sahrens case ZFS_PROP_DEVICES: 2136fa9e4066Sahrens case ZFS_PROP_EXEC: 21373ccfa83cSahrens case ZFS_PROP_READONLY: 21383ccfa83cSahrens case ZFS_PROP_SETUID: 21393ccfa83cSahrens case ZFS_PROP_XATTR: 2140da6c28aaSamw case ZFS_PROP_NBMAND: 214199653d4eSeschrock *val = getprop_uint64(zhp, prop, source); 2142fa9e4066Sahrens 214392241e0bSTom Erickson if (received) 214492241e0bSTom Erickson break; 214592241e0bSTom Erickson 21463ccfa83cSahrens if (hasmntopt(&mnt, mntopt_on) && !*val) { 214799653d4eSeschrock *val = B_TRUE; 2148fa9e4066Sahrens if (src) 2149990b4856Slling *src = ZPROP_SRC_TEMPORARY; 21503ccfa83cSahrens } else if (hasmntopt(&mnt, mntopt_off) && *val) { 215199653d4eSeschrock *val = B_FALSE; 2152fa9e4066Sahrens if (src) 2153990b4856Slling *src = ZPROP_SRC_TEMPORARY; 2154fa9e4066Sahrens } 215599653d4eSeschrock break; 2156fa9e4066Sahrens 21573ccfa83cSahrens case ZFS_PROP_CANMOUNT: 2158d41c4376SMark J Musante case ZFS_PROP_VOLSIZE: 2159fa9e4066Sahrens case ZFS_PROP_QUOTA: 2160a9799022Sck case ZFS_PROP_REFQUOTA: 2161fa9e4066Sahrens case ZFS_PROP_RESERVATION: 2162a9799022Sck case ZFS_PROP_REFRESERVATION: 2163a2afb611SJerry Jelinek case ZFS_PROP_FILESYSTEM_LIMIT: 2164a2afb611SJerry Jelinek case ZFS_PROP_SNAPSHOT_LIMIT: 2165a2afb611SJerry Jelinek case ZFS_PROP_FILESYSTEM_COUNT: 2166a2afb611SJerry Jelinek case ZFS_PROP_SNAPSHOT_COUNT: 2167a2eea2e1Sahrens *val = getprop_uint64(zhp, prop, source); 216892241e0bSTom Erickson 216992241e0bSTom Erickson if (*source == NULL) { 217092241e0bSTom Erickson /* not default, must be local */ 2171fa9e4066Sahrens *source = zhp->zfs_name; 217292241e0bSTom Erickson } 217399653d4eSeschrock break; 2174fa9e4066Sahrens 2175fa9e4066Sahrens case ZFS_PROP_MOUNTED: 217699653d4eSeschrock *val = (zhp->zfs_mntopts != NULL); 217799653d4eSeschrock break; 2178fa9e4066Sahrens 217939c23413Seschrock case ZFS_PROP_NUMCLONES: 218039c23413Seschrock *val = zhp->zfs_dmustats.dds_num_clones; 218139c23413Seschrock break; 218239c23413Seschrock 2183bd00f61bSrm case ZFS_PROP_VERSION: 2184de8267e0Stimh case ZFS_PROP_NORMALIZE: 2185de8267e0Stimh case ZFS_PROP_UTF8ONLY: 2186de8267e0Stimh case ZFS_PROP_CASE: 2187de8267e0Stimh if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) || 2188de8267e0Stimh zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 21893d7934e1Srm return (-1); 2190bd00f61bSrm (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2191de8267e0Stimh if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) { 2192de8267e0Stimh zcmd_free_nvlists(&zc); 2193f4b94bdeSMatthew Ahrens return (-1); 2194bd00f61bSrm } 2195de8267e0Stimh if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 || 2196de8267e0Stimh nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop), 2197de8267e0Stimh val) != 0) { 2198de8267e0Stimh zcmd_free_nvlists(&zc); 2199f4b94bdeSMatthew Ahrens return (-1); 2200de8267e0Stimh } 2201aab83bb8SJosef 'Jeff' Sipek nvlist_free(zplprops); 2202de8267e0Stimh zcmd_free_nvlists(&zc); 2203bd00f61bSrm break; 2204bd00f61bSrm 2205ca48f36fSKeith M Wesolowski case ZFS_PROP_INCONSISTENT: 2206ca48f36fSKeith M Wesolowski *val = zhp->zfs_dmustats.dds_inconsistent; 2207ca48f36fSKeith M Wesolowski break; 2208ca48f36fSKeith M Wesolowski 2209fa9e4066Sahrens default: 2210e7437265Sahrens switch (zfs_prop_get_type(prop)) { 221191ebeef5Sahrens case PROP_TYPE_NUMBER: 221291ebeef5Sahrens case PROP_TYPE_INDEX: 2213e7437265Sahrens *val = getprop_uint64(zhp, prop, source); 221474e7dc98SMatthew Ahrens /* 221514843421SMatthew Ahrens * If we tried to use a default value for a 221674e7dc98SMatthew Ahrens * readonly property, it means that it was not 22174d86c0eaSMatthew Ahrens * present. Note this only applies to "truly" 22184d86c0eaSMatthew Ahrens * readonly properties, not set-once properties 22194d86c0eaSMatthew Ahrens * like volblocksize. 222074e7dc98SMatthew Ahrens */ 222174e7dc98SMatthew Ahrens if (zfs_prop_readonly(prop) && 22224d86c0eaSMatthew Ahrens !zfs_prop_setonce(prop) && 222331f572c2STom Erickson *source != NULL && (*source)[0] == '\0') { 222431f572c2STom Erickson *source = NULL; 2225ad2760acSMatthew Ahrens return (-1); 222674e7dc98SMatthew Ahrens } 2227e7437265Sahrens break; 2228e7437265Sahrens 222991ebeef5Sahrens case PROP_TYPE_STRING: 2230e7437265Sahrens default: 2231e7437265Sahrens zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 2232e7437265Sahrens "cannot get non-numeric property")); 2233e7437265Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP, 2234e7437265Sahrens dgettext(TEXT_DOMAIN, "internal error"))); 2235e7437265Sahrens } 2236fa9e4066Sahrens } 2237fa9e4066Sahrens 2238fa9e4066Sahrens return (0); 2239fa9e4066Sahrens } 2240fa9e4066Sahrens 2241fa9e4066Sahrens /* 2242fa9e4066Sahrens * Calculate the source type, given the raw source string. 2243fa9e4066Sahrens */ 2244fa9e4066Sahrens static void 2245990b4856Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source, 2246fa9e4066Sahrens char *statbuf, size_t statlen) 2247fa9e4066Sahrens { 2248990b4856Slling if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY) 2249fa9e4066Sahrens return; 2250fa9e4066Sahrens 2251fa9e4066Sahrens if (source == NULL) { 2252990b4856Slling *srctype = ZPROP_SRC_NONE; 2253fa9e4066Sahrens } else if (source[0] == '\0') { 2254990b4856Slling *srctype = ZPROP_SRC_DEFAULT; 225592241e0bSTom Erickson } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) { 225692241e0bSTom Erickson *srctype = ZPROP_SRC_RECEIVED; 2257fa9e4066Sahrens } else { 2258fa9e4066Sahrens if (strcmp(source, zhp->zfs_name) == 0) { 2259990b4856Slling *srctype = ZPROP_SRC_LOCAL; 2260fa9e4066Sahrens } else { 2261fa9e4066Sahrens (void) strlcpy(statbuf, source, statlen); 2262990b4856Slling *srctype = ZPROP_SRC_INHERITED; 2263fa9e4066Sahrens } 2264fa9e4066Sahrens } 2265fa9e4066Sahrens 2266fa9e4066Sahrens } 2267fa9e4066Sahrens 226892241e0bSTom Erickson int 226992241e0bSTom Erickson zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf, 227092241e0bSTom Erickson size_t proplen, boolean_t literal) 227192241e0bSTom Erickson { 227292241e0bSTom Erickson zfs_prop_t prop; 227392241e0bSTom Erickson int err = 0; 227492241e0bSTom Erickson 227592241e0bSTom Erickson if (zhp->zfs_recvd_props == NULL) 227692241e0bSTom Erickson if (get_recvd_props_ioctl(zhp) != 0) 227792241e0bSTom Erickson return (-1); 227892241e0bSTom Erickson 227992241e0bSTom Erickson prop = zfs_name_to_prop(propname); 228092241e0bSTom Erickson 228192241e0bSTom Erickson if (prop != ZPROP_INVAL) { 228292241e0bSTom Erickson uint64_t cookie; 228392241e0bSTom Erickson if (!nvlist_exists(zhp->zfs_recvd_props, propname)) 228492241e0bSTom Erickson return (-1); 228592241e0bSTom Erickson zfs_set_recvd_props_mode(zhp, &cookie); 228692241e0bSTom Erickson err = zfs_prop_get(zhp, prop, propbuf, proplen, 228792241e0bSTom Erickson NULL, NULL, 0, literal); 228892241e0bSTom Erickson zfs_unset_recvd_props_mode(zhp, &cookie); 228992241e0bSTom Erickson } else { 229092241e0bSTom Erickson nvlist_t *propval; 229192241e0bSTom Erickson char *recvdval; 229292241e0bSTom Erickson if (nvlist_lookup_nvlist(zhp->zfs_recvd_props, 229392241e0bSTom Erickson propname, &propval) != 0) 229492241e0bSTom Erickson return (-1); 229592241e0bSTom Erickson verify(nvlist_lookup_string(propval, ZPROP_VALUE, 229692241e0bSTom Erickson &recvdval) == 0); 229792241e0bSTom Erickson (void) strlcpy(propbuf, recvdval, proplen); 229892241e0bSTom Erickson } 229992241e0bSTom Erickson 230092241e0bSTom Erickson return (err == 0 ? 0 : -1); 230192241e0bSTom Erickson } 230292241e0bSTom Erickson 230319b94df9SMatthew Ahrens static int 230419b94df9SMatthew Ahrens get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen) 230519b94df9SMatthew Ahrens { 230619b94df9SMatthew Ahrens nvlist_t *value; 230719b94df9SMatthew Ahrens nvpair_t *pair; 230819b94df9SMatthew Ahrens 230919b94df9SMatthew Ahrens value = zfs_get_clones_nvl(zhp); 231019b94df9SMatthew Ahrens if (value == NULL) 231119b94df9SMatthew Ahrens return (-1); 231219b94df9SMatthew Ahrens 231319b94df9SMatthew Ahrens propbuf[0] = '\0'; 231419b94df9SMatthew Ahrens for (pair = nvlist_next_nvpair(value, NULL); pair != NULL; 231519b94df9SMatthew Ahrens pair = nvlist_next_nvpair(value, pair)) { 231619b94df9SMatthew Ahrens if (propbuf[0] != '\0') 231719b94df9SMatthew Ahrens (void) strlcat(propbuf, ",", proplen); 231819b94df9SMatthew Ahrens (void) strlcat(propbuf, nvpair_name(pair), proplen); 231919b94df9SMatthew Ahrens } 232019b94df9SMatthew Ahrens 232119b94df9SMatthew Ahrens return (0); 232219b94df9SMatthew Ahrens } 232319b94df9SMatthew Ahrens 232419b94df9SMatthew Ahrens struct get_clones_arg { 232519b94df9SMatthew Ahrens uint64_t numclones; 232619b94df9SMatthew Ahrens nvlist_t *value; 232719b94df9SMatthew Ahrens const char *origin; 23289adfa60dSMatthew Ahrens char buf[ZFS_MAX_DATASET_NAME_LEN]; 232919b94df9SMatthew Ahrens }; 233019b94df9SMatthew Ahrens 233119b94df9SMatthew Ahrens int 233219b94df9SMatthew Ahrens get_clones_cb(zfs_handle_t *zhp, void *arg) 233319b94df9SMatthew Ahrens { 233419b94df9SMatthew Ahrens struct get_clones_arg *gca = arg; 233519b94df9SMatthew Ahrens 233619b94df9SMatthew Ahrens if (gca->numclones == 0) { 233719b94df9SMatthew Ahrens zfs_close(zhp); 233819b94df9SMatthew Ahrens return (0); 233919b94df9SMatthew Ahrens } 234019b94df9SMatthew Ahrens 234119b94df9SMatthew Ahrens if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf), 234219b94df9SMatthew Ahrens NULL, NULL, 0, B_TRUE) != 0) 234319b94df9SMatthew Ahrens goto out; 234419b94df9SMatthew Ahrens if (strcmp(gca->buf, gca->origin) == 0) { 23453b2aab18SMatthew Ahrens fnvlist_add_boolean(gca->value, zfs_get_name(zhp)); 234619b94df9SMatthew Ahrens gca->numclones--; 234719b94df9SMatthew Ahrens } 234819b94df9SMatthew Ahrens 234919b94df9SMatthew Ahrens out: 235019b94df9SMatthew Ahrens (void) zfs_iter_children(zhp, get_clones_cb, gca); 235119b94df9SMatthew Ahrens zfs_close(zhp); 235219b94df9SMatthew Ahrens return (0); 235319b94df9SMatthew Ahrens } 235419b94df9SMatthew Ahrens 235519b94df9SMatthew Ahrens nvlist_t * 235619b94df9SMatthew Ahrens zfs_get_clones_nvl(zfs_handle_t *zhp) 235719b94df9SMatthew Ahrens { 235819b94df9SMatthew Ahrens nvlist_t *nv, *value; 235919b94df9SMatthew Ahrens 236019b94df9SMatthew Ahrens if (nvlist_lookup_nvlist(zhp->zfs_props, 236119b94df9SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) { 236219b94df9SMatthew Ahrens struct get_clones_arg gca; 236319b94df9SMatthew Ahrens 236419b94df9SMatthew Ahrens /* 236519b94df9SMatthew Ahrens * if this is a snapshot, then the kernel wasn't able 236619b94df9SMatthew Ahrens * to get the clones. Do it by slowly iterating. 236719b94df9SMatthew Ahrens */ 236819b94df9SMatthew Ahrens if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) 236919b94df9SMatthew Ahrens return (NULL); 237019b94df9SMatthew Ahrens if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0) 237119b94df9SMatthew Ahrens return (NULL); 237219b94df9SMatthew Ahrens if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) { 237319b94df9SMatthew Ahrens nvlist_free(nv); 237419b94df9SMatthew Ahrens return (NULL); 237519b94df9SMatthew Ahrens } 237619b94df9SMatthew Ahrens 237719b94df9SMatthew Ahrens gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES); 237819b94df9SMatthew Ahrens gca.value = value; 237919b94df9SMatthew Ahrens gca.origin = zhp->zfs_name; 238019b94df9SMatthew Ahrens 238119b94df9SMatthew Ahrens if (gca.numclones != 0) { 238219b94df9SMatthew Ahrens zfs_handle_t *root; 23839adfa60dSMatthew Ahrens char pool[ZFS_MAX_DATASET_NAME_LEN]; 238419b94df9SMatthew Ahrens char *cp = pool; 238519b94df9SMatthew Ahrens 238619b94df9SMatthew Ahrens /* get the pool name */ 238719b94df9SMatthew Ahrens (void) strlcpy(pool, zhp->zfs_name, sizeof (pool)); 238819b94df9SMatthew Ahrens (void) strsep(&cp, "/@"); 238919b94df9SMatthew Ahrens root = zfs_open(zhp->zfs_hdl, pool, 239019b94df9SMatthew Ahrens ZFS_TYPE_FILESYSTEM); 239119b94df9SMatthew Ahrens 239219b94df9SMatthew Ahrens (void) get_clones_cb(root, &gca); 239319b94df9SMatthew Ahrens } 239419b94df9SMatthew Ahrens 239519b94df9SMatthew Ahrens if (gca.numclones != 0 || 239619b94df9SMatthew Ahrens nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 || 239719b94df9SMatthew Ahrens nvlist_add_nvlist(zhp->zfs_props, 239819b94df9SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) { 239919b94df9SMatthew Ahrens nvlist_free(nv); 240019b94df9SMatthew Ahrens nvlist_free(value); 240119b94df9SMatthew Ahrens return (NULL); 240219b94df9SMatthew Ahrens } 240319b94df9SMatthew Ahrens nvlist_free(nv); 240419b94df9SMatthew Ahrens nvlist_free(value); 240519b94df9SMatthew Ahrens verify(0 == nvlist_lookup_nvlist(zhp->zfs_props, 240619b94df9SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_CLONES), &nv)); 240719b94df9SMatthew Ahrens } 240819b94df9SMatthew Ahrens 240919b94df9SMatthew Ahrens verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0); 241019b94df9SMatthew Ahrens 241119b94df9SMatthew Ahrens return (value); 241219b94df9SMatthew Ahrens } 241319b94df9SMatthew Ahrens 2414dfc11533SChris Williamson /* 2415dfc11533SChris Williamson * Accepts a property and value and checks that the value 2416dfc11533SChris Williamson * matches the one found by the channel program. If they are 2417dfc11533SChris Williamson * not equal, print both of them. 2418dfc11533SChris Williamson */ 2419dfc11533SChris Williamson void 2420dfc11533SChris Williamson zcp_check(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t intval, 2421dfc11533SChris Williamson const char *strval) 2422dfc11533SChris Williamson { 2423dfc11533SChris Williamson if (!zhp->zfs_hdl->libzfs_prop_debug) 2424dfc11533SChris Williamson return; 2425dfc11533SChris Williamson int error; 2426dfc11533SChris Williamson char *poolname = zhp->zpool_hdl->zpool_name; 2427dfc11533SChris Williamson const char *program = 2428dfc11533SChris Williamson "args = ...\n" 2429dfc11533SChris Williamson "ds = args['dataset']\n" 2430dfc11533SChris Williamson "prop = args['property']\n" 2431dfc11533SChris Williamson "value, setpoint = zfs.get_prop(ds, prop)\n" 2432dfc11533SChris Williamson "return {value=value, setpoint=setpoint}\n"; 2433dfc11533SChris Williamson nvlist_t *outnvl; 2434dfc11533SChris Williamson nvlist_t *retnvl; 2435dfc11533SChris Williamson nvlist_t *argnvl = fnvlist_alloc(); 2436dfc11533SChris Williamson 2437dfc11533SChris Williamson fnvlist_add_string(argnvl, "dataset", zhp->zfs_name); 2438dfc11533SChris Williamson fnvlist_add_string(argnvl, "property", zfs_prop_to_name(prop)); 2439dfc11533SChris Williamson 2440a3b28680SSerapheim Dimitropoulos error = lzc_channel_program_nosync(poolname, program, 2441dfc11533SChris Williamson 10 * 1000 * 1000, 10 * 1024 * 1024, argnvl, &outnvl); 2442dfc11533SChris Williamson 2443dfc11533SChris Williamson if (error == 0) { 2444dfc11533SChris Williamson retnvl = fnvlist_lookup_nvlist(outnvl, "return"); 2445dfc11533SChris Williamson if (zfs_prop_get_type(prop) == PROP_TYPE_NUMBER) { 2446dfc11533SChris Williamson int64_t ans; 2447dfc11533SChris Williamson error = nvlist_lookup_int64(retnvl, "value", &ans); 2448dfc11533SChris Williamson if (error != 0) { 2449dfc11533SChris Williamson (void) fprintf(stderr, "zcp check error: %u\n", 2450dfc11533SChris Williamson error); 2451dfc11533SChris Williamson return; 2452dfc11533SChris Williamson } 2453dfc11533SChris Williamson if (ans != intval) { 2454dfc11533SChris Williamson (void) fprintf(stderr, 2455dfc11533SChris Williamson "%s: zfs found %lld, but zcp found %lld\n", 2456dfc11533SChris Williamson zfs_prop_to_name(prop), 2457dfc11533SChris Williamson (longlong_t)intval, (longlong_t)ans); 2458dfc11533SChris Williamson } 2459dfc11533SChris Williamson } else { 2460dfc11533SChris Williamson char *str_ans; 2461dfc11533SChris Williamson error = nvlist_lookup_string(retnvl, "value", &str_ans); 2462dfc11533SChris Williamson if (error != 0) { 2463dfc11533SChris Williamson (void) fprintf(stderr, "zcp check error: %u\n", 2464dfc11533SChris Williamson error); 2465dfc11533SChris Williamson return; 2466dfc11533SChris Williamson } 2467dfc11533SChris Williamson if (strcmp(strval, str_ans) != 0) { 2468dfc11533SChris Williamson (void) fprintf(stderr, 2469dfc11533SChris Williamson "%s: zfs found %s, but zcp found %s\n", 2470dfc11533SChris Williamson zfs_prop_to_name(prop), 2471dfc11533SChris Williamson strval, str_ans); 2472dfc11533SChris Williamson } 2473dfc11533SChris Williamson } 2474dfc11533SChris Williamson } else { 2475dfc11533SChris Williamson (void) fprintf(stderr, 2476dfc11533SChris Williamson "zcp check failed, channel program error: %u\n", error); 2477dfc11533SChris Williamson } 2478dfc11533SChris Williamson nvlist_free(argnvl); 2479dfc11533SChris Williamson nvlist_free(outnvl); 2480dfc11533SChris Williamson } 2481dfc11533SChris Williamson 2482fa9e4066Sahrens /* 2483fa9e4066Sahrens * Retrieve a property from the given object. If 'literal' is specified, then 2484fa9e4066Sahrens * numbers are left as exact values. Otherwise, numbers are converted to a 2485fa9e4066Sahrens * human-readable form. 2486fa9e4066Sahrens * 2487fa9e4066Sahrens * Returns 0 on success, or -1 on error. 2488fa9e4066Sahrens */ 2489fa9e4066Sahrens int 2490fa9e4066Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen, 2491990b4856Slling zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal) 2492fa9e4066Sahrens { 2493fa9e4066Sahrens char *source = NULL; 2494fa9e4066Sahrens uint64_t val; 24959c3fd121SMatthew Ahrens const char *str; 2496e9dbad6fSeschrock const char *strval; 249792241e0bSTom Erickson boolean_t received = zfs_is_recvd_props_mode(zhp); 2498fa9e4066Sahrens 2499fa9e4066Sahrens /* 2500fa9e4066Sahrens * Check to see if this property applies to our object 2501fa9e4066Sahrens */ 2502fa9e4066Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 2503fa9e4066Sahrens return (-1); 2504fa9e4066Sahrens 250592241e0bSTom Erickson if (received && zfs_prop_readonly(prop)) 250692241e0bSTom Erickson return (-1); 250792241e0bSTom Erickson 2508fa9e4066Sahrens if (src) 2509990b4856Slling *src = ZPROP_SRC_NONE; 2510fa9e4066Sahrens 2511fa9e4066Sahrens switch (prop) { 2512fa9e4066Sahrens case ZFS_PROP_CREATION: 2513fa9e4066Sahrens /* 2514fa9e4066Sahrens * 'creation' is a time_t stored in the statistics. We convert 2515fa9e4066Sahrens * this into a string unless 'literal' is specified. 2516fa9e4066Sahrens */ 2517fa9e4066Sahrens { 2518a2eea2e1Sahrens val = getprop_uint64(zhp, prop, &source); 2519a2eea2e1Sahrens time_t time = (time_t)val; 2520fa9e4066Sahrens struct tm t; 2521fa9e4066Sahrens 2522fa9e4066Sahrens if (literal || 2523fa9e4066Sahrens localtime_r(&time, &t) == NULL || 2524fa9e4066Sahrens strftime(propbuf, proplen, "%a %b %e %k:%M %Y", 2525fa9e4066Sahrens &t) == 0) 2526a2eea2e1Sahrens (void) snprintf(propbuf, proplen, "%llu", val); 2527fa9e4066Sahrens } 2528dfc11533SChris Williamson zcp_check(zhp, prop, val, NULL); 2529fa9e4066Sahrens break; 2530fa9e4066Sahrens 2531fa9e4066Sahrens case ZFS_PROP_MOUNTPOINT: 2532fa9e4066Sahrens /* 2533fa9e4066Sahrens * Getting the precise mountpoint can be tricky. 2534fa9e4066Sahrens * 2535fa9e4066Sahrens * - for 'none' or 'legacy', return those values. 2536fa9e4066Sahrens * - for inherited mountpoints, we want to take everything 2537fa9e4066Sahrens * after our ancestor and append it to the inherited value. 2538fa9e4066Sahrens * 2539fa9e4066Sahrens * If the pool has an alternate root, we want to prepend that 2540fa9e4066Sahrens * root to any values we return. 2541fa9e4066Sahrens */ 254229ab75c9Srm 25437f7322feSeschrock str = getprop_string(zhp, prop, &source); 2544fa9e4066Sahrens 2545b21718f0Sgw if (str[0] == '/') { 254629ab75c9Srm char buf[MAXPATHLEN]; 254729ab75c9Srm char *root = buf; 2548a79992aaSTom Erickson const char *relpath; 2549fa9e4066Sahrens 2550a79992aaSTom Erickson /* 2551a79992aaSTom Erickson * If we inherit the mountpoint, even from a dataset 2552a79992aaSTom Erickson * with a received value, the source will be the path of 2553a79992aaSTom Erickson * the dataset we inherit from. If source is 2554a79992aaSTom Erickson * ZPROP_SOURCE_VAL_RECVD, the received value is not 2555a79992aaSTom Erickson * inherited. 2556a79992aaSTom Erickson */ 2557a79992aaSTom Erickson if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) { 2558a79992aaSTom Erickson relpath = ""; 2559a79992aaSTom Erickson } else { 2560a79992aaSTom Erickson relpath = zhp->zfs_name + strlen(source); 2561a79992aaSTom Erickson if (relpath[0] == '/') 2562a79992aaSTom Erickson relpath++; 2563a79992aaSTom Erickson } 2564b21718f0Sgw 256529ab75c9Srm if ((zpool_get_prop(zhp->zpool_hdl, 2566c58b3526SAdam Stevko ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL, 2567c58b3526SAdam Stevko B_FALSE)) || (strcmp(root, "-") == 0)) 256829ab75c9Srm root[0] = '\0'; 2569b21718f0Sgw /* 2570b21718f0Sgw * Special case an alternate root of '/'. This will 2571b21718f0Sgw * avoid having multiple leading slashes in the 2572b21718f0Sgw * mountpoint path. 2573b21718f0Sgw */ 2574b21718f0Sgw if (strcmp(root, "/") == 0) 2575b21718f0Sgw root++; 2576b21718f0Sgw 2577b21718f0Sgw /* 2578b21718f0Sgw * If the mountpoint is '/' then skip over this 2579b21718f0Sgw * if we are obtaining either an alternate root or 2580b21718f0Sgw * an inherited mountpoint. 2581b21718f0Sgw */ 2582b21718f0Sgw if (str[1] == '\0' && (root[0] != '\0' || 2583b21718f0Sgw relpath[0] != '\0')) 25847f7322feSeschrock str++; 2585fa9e4066Sahrens 2586fa9e4066Sahrens if (relpath[0] == '\0') 2587fa9e4066Sahrens (void) snprintf(propbuf, proplen, "%s%s", 25887f7322feSeschrock root, str); 2589fa9e4066Sahrens else 2590fa9e4066Sahrens (void) snprintf(propbuf, proplen, "%s%s%s%s", 25917f7322feSeschrock root, str, relpath[0] == '@' ? "" : "/", 2592fa9e4066Sahrens relpath); 2593fa9e4066Sahrens } else { 2594fa9e4066Sahrens /* 'legacy' or 'none' */ 25957f7322feSeschrock (void) strlcpy(propbuf, str, proplen); 2596fa9e4066Sahrens } 2597dfc11533SChris Williamson zcp_check(zhp, prop, NULL, propbuf); 2598fa9e4066Sahrens break; 2599fa9e4066Sahrens 2600fa9e4066Sahrens case ZFS_PROP_ORIGIN: 26019c3fd121SMatthew Ahrens str = getprop_string(zhp, prop, &source); 26029c3fd121SMatthew Ahrens if (str == NULL) 2603fa9e4066Sahrens return (-1); 26049c3fd121SMatthew Ahrens (void) strlcpy(propbuf, str, proplen); 2605dfc11533SChris Williamson zcp_check(zhp, prop, NULL, str); 2606fa9e4066Sahrens break; 2607fa9e4066Sahrens 260819b94df9SMatthew Ahrens case ZFS_PROP_CLONES: 260919b94df9SMatthew Ahrens if (get_clones_string(zhp, propbuf, proplen) != 0) 261019b94df9SMatthew Ahrens return (-1); 261119b94df9SMatthew Ahrens break; 261219b94df9SMatthew Ahrens 2613fa9e4066Sahrens case ZFS_PROP_QUOTA: 2614a9799022Sck case ZFS_PROP_REFQUOTA: 2615fa9e4066Sahrens case ZFS_PROP_RESERVATION: 2616a9799022Sck case ZFS_PROP_REFRESERVATION: 2617a9799022Sck 261899653d4eSeschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 261999653d4eSeschrock return (-1); 2620fa9e4066Sahrens /* 2621fa9e4066Sahrens * If quota or reservation is 0, we translate this into 'none' 2622fa9e4066Sahrens * (unless literal is set), and indicate that it's the default 2623fa9e4066Sahrens * value. Otherwise, we print the number nicely and indicate 2624fa9e4066Sahrens * that its set locally. 2625fa9e4066Sahrens */ 2626fa9e4066Sahrens if (val == 0) { 2627fa9e4066Sahrens if (literal) 2628fa9e4066Sahrens (void) strlcpy(propbuf, "0", proplen); 2629fa9e4066Sahrens else 2630fa9e4066Sahrens (void) strlcpy(propbuf, "none", proplen); 2631fa9e4066Sahrens } else { 2632fa9e4066Sahrens if (literal) 26335ad82045Snd (void) snprintf(propbuf, proplen, "%llu", 2634b1b8ab34Slling (u_longlong_t)val); 2635fa9e4066Sahrens else 2636fa9e4066Sahrens zfs_nicenum(val, propbuf, proplen); 2637fa9e4066Sahrens } 2638dfc11533SChris Williamson zcp_check(zhp, prop, val, NULL); 2639fa9e4066Sahrens break; 2640fa9e4066Sahrens 2641a2afb611SJerry Jelinek case ZFS_PROP_FILESYSTEM_LIMIT: 2642a2afb611SJerry Jelinek case ZFS_PROP_SNAPSHOT_LIMIT: 2643a2afb611SJerry Jelinek case ZFS_PROP_FILESYSTEM_COUNT: 2644a2afb611SJerry Jelinek case ZFS_PROP_SNAPSHOT_COUNT: 2645a2afb611SJerry Jelinek 2646a2afb611SJerry Jelinek if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 2647a2afb611SJerry Jelinek return (-1); 2648a2afb611SJerry Jelinek 2649a2afb611SJerry Jelinek /* 2650a2afb611SJerry Jelinek * If limit is UINT64_MAX, we translate this into 'none' (unless 2651a2afb611SJerry Jelinek * literal is set), and indicate that it's the default value. 2652a2afb611SJerry Jelinek * Otherwise, we print the number nicely and indicate that it's 2653a2afb611SJerry Jelinek * set locally. 2654a2afb611SJerry Jelinek */ 2655a2afb611SJerry Jelinek if (literal) { 2656a2afb611SJerry Jelinek (void) snprintf(propbuf, proplen, "%llu", 2657a2afb611SJerry Jelinek (u_longlong_t)val); 2658a2afb611SJerry Jelinek } else if (val == UINT64_MAX) { 2659a2afb611SJerry Jelinek (void) strlcpy(propbuf, "none", proplen); 2660a2afb611SJerry Jelinek } else { 2661a2afb611SJerry Jelinek zfs_nicenum(val, propbuf, proplen); 2662a2afb611SJerry Jelinek } 2663dfc11533SChris Williamson 2664dfc11533SChris Williamson zcp_check(zhp, prop, val, NULL); 2665a2afb611SJerry Jelinek break; 2666a2afb611SJerry Jelinek 2667187d6ac0SMatt Ahrens case ZFS_PROP_REFRATIO: 2668fa9e4066Sahrens case ZFS_PROP_COMPRESSRATIO: 266999653d4eSeschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 267099653d4eSeschrock return (-1); 2671b24ab676SJeff Bonwick (void) snprintf(propbuf, proplen, "%llu.%02llux", 2672b24ab676SJeff Bonwick (u_longlong_t)(val / 100), 2673b24ab676SJeff Bonwick (u_longlong_t)(val % 100)); 2674dfc11533SChris Williamson zcp_check(zhp, prop, val, NULL); 2675fa9e4066Sahrens break; 2676fa9e4066Sahrens 2677fa9e4066Sahrens case ZFS_PROP_TYPE: 2678fa9e4066Sahrens switch (zhp->zfs_type) { 2679fa9e4066Sahrens case ZFS_TYPE_FILESYSTEM: 2680fa9e4066Sahrens str = "filesystem"; 2681fa9e4066Sahrens break; 2682fa9e4066Sahrens case ZFS_TYPE_VOLUME: 2683fa9e4066Sahrens str = "volume"; 2684fa9e4066Sahrens break; 2685fa9e4066Sahrens case ZFS_TYPE_SNAPSHOT: 2686fa9e4066Sahrens str = "snapshot"; 2687fa9e4066Sahrens break; 268878f17100SMatthew Ahrens case ZFS_TYPE_BOOKMARK: 268978f17100SMatthew Ahrens str = "bookmark"; 269078f17100SMatthew Ahrens break; 2691fa9e4066Sahrens default: 269299653d4eSeschrock abort(); 2693fa9e4066Sahrens } 2694fa9e4066Sahrens (void) snprintf(propbuf, proplen, "%s", str); 2695dfc11533SChris Williamson zcp_check(zhp, prop, NULL, propbuf); 2696fa9e4066Sahrens break; 2697fa9e4066Sahrens 2698fa9e4066Sahrens case ZFS_PROP_MOUNTED: 2699fa9e4066Sahrens /* 2700fa9e4066Sahrens * The 'mounted' property is a pseudo-property that described 2701fa9e4066Sahrens * whether the filesystem is currently mounted. Even though 2702fa9e4066Sahrens * it's a boolean value, the typical values of "on" and "off" 2703fa9e4066Sahrens * don't make sense, so we translate to "yes" and "no". 2704fa9e4066Sahrens */ 270599653d4eSeschrock if (get_numeric_property(zhp, ZFS_PROP_MOUNTED, 270699653d4eSeschrock src, &source, &val) != 0) 270799653d4eSeschrock return (-1); 270899653d4eSeschrock if (val) 2709fa9e4066Sahrens (void) strlcpy(propbuf, "yes", proplen); 2710fa9e4066Sahrens else 2711fa9e4066Sahrens (void) strlcpy(propbuf, "no", proplen); 2712fa9e4066Sahrens break; 2713fa9e4066Sahrens 2714fa9e4066Sahrens case ZFS_PROP_NAME: 2715fa9e4066Sahrens /* 2716fa9e4066Sahrens * The 'name' property is a pseudo-property derived from the 2717fa9e4066Sahrens * dataset name. It is presented as a real property to simplify 2718fa9e4066Sahrens * consumers. 2719fa9e4066Sahrens */ 2720fa9e4066Sahrens (void) strlcpy(propbuf, zhp->zfs_name, proplen); 2721dfc11533SChris Williamson zcp_check(zhp, prop, NULL, propbuf); 2722fa9e4066Sahrens break; 2723fa9e4066Sahrens 27244201a95eSRic Aleshire case ZFS_PROP_MLSLABEL: 27254201a95eSRic Aleshire { 27264201a95eSRic Aleshire m_label_t *new_sl = NULL; 27274201a95eSRic Aleshire char *ascii = NULL; /* human readable label */ 27284201a95eSRic Aleshire 27294201a95eSRic Aleshire (void) strlcpy(propbuf, 27304201a95eSRic Aleshire getprop_string(zhp, prop, &source), proplen); 27314201a95eSRic Aleshire 27324201a95eSRic Aleshire if (literal || (strcasecmp(propbuf, 27334201a95eSRic Aleshire ZFS_MLSLABEL_DEFAULT) == 0)) 27344201a95eSRic Aleshire break; 27354201a95eSRic Aleshire 27364201a95eSRic Aleshire /* 27374201a95eSRic Aleshire * Try to translate the internal hex string to 27384201a95eSRic Aleshire * human-readable output. If there are any 27394201a95eSRic Aleshire * problems just use the hex string. 27404201a95eSRic Aleshire */ 27414201a95eSRic Aleshire 27424201a95eSRic Aleshire if (str_to_label(propbuf, &new_sl, MAC_LABEL, 27434201a95eSRic Aleshire L_NO_CORRECTION, NULL) == -1) { 27444201a95eSRic Aleshire m_label_free(new_sl); 27454201a95eSRic Aleshire break; 27464201a95eSRic Aleshire } 27474201a95eSRic Aleshire 27484201a95eSRic Aleshire if (label_to_str(new_sl, &ascii, M_LABEL, 27494201a95eSRic Aleshire DEF_NAMES) != 0) { 27504201a95eSRic Aleshire if (ascii) 27514201a95eSRic Aleshire free(ascii); 27524201a95eSRic Aleshire m_label_free(new_sl); 27534201a95eSRic Aleshire break; 27544201a95eSRic Aleshire } 27554201a95eSRic Aleshire m_label_free(new_sl); 27564201a95eSRic Aleshire 27574201a95eSRic Aleshire (void) strlcpy(propbuf, ascii, proplen); 27584201a95eSRic Aleshire free(ascii); 27594201a95eSRic Aleshire } 27604201a95eSRic Aleshire break; 27614201a95eSRic Aleshire 2762f0f3ef5aSGarrett D'Amore case ZFS_PROP_GUID: 2763e8d4a73cSJosh Paetzel case ZFS_PROP_CREATETXG: 2764f0f3ef5aSGarrett D'Amore /* 2765f0f3ef5aSGarrett D'Amore * GUIDs are stored as numbers, but they are identifiers. 2766f0f3ef5aSGarrett D'Amore * We don't want them to be pretty printed, because pretty 2767f0f3ef5aSGarrett D'Amore * printing mangles the ID into a truncated and useless value. 2768f0f3ef5aSGarrett D'Amore */ 2769f0f3ef5aSGarrett D'Amore if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 2770f0f3ef5aSGarrett D'Amore return (-1); 2771f0f3ef5aSGarrett D'Amore (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val); 2772dfc11533SChris Williamson zcp_check(zhp, prop, val, NULL); 2773f0f3ef5aSGarrett D'Amore break; 2774f0f3ef5aSGarrett D'Amore 2775fa9e4066Sahrens default: 2776e7437265Sahrens switch (zfs_prop_get_type(prop)) { 277791ebeef5Sahrens case PROP_TYPE_NUMBER: 2778e7437265Sahrens if (get_numeric_property(zhp, prop, src, 2779dfc11533SChris Williamson &source, &val) != 0) { 2780e7437265Sahrens return (-1); 2781dfc11533SChris Williamson } 2782dfc11533SChris Williamson 2783dfc11533SChris Williamson if (literal) { 2784e7437265Sahrens (void) snprintf(propbuf, proplen, "%llu", 2785e7437265Sahrens (u_longlong_t)val); 2786dfc11533SChris Williamson } else { 2787e7437265Sahrens zfs_nicenum(val, propbuf, proplen); 2788dfc11533SChris Williamson } 2789dfc11533SChris Williamson zcp_check(zhp, prop, val, NULL); 2790e7437265Sahrens break; 2791e7437265Sahrens 279291ebeef5Sahrens case PROP_TYPE_STRING: 27939c3fd121SMatthew Ahrens str = getprop_string(zhp, prop, &source); 27949c3fd121SMatthew Ahrens if (str == NULL) 27959c3fd121SMatthew Ahrens return (-1); 2796dfc11533SChris Williamson 27979c3fd121SMatthew Ahrens (void) strlcpy(propbuf, str, proplen); 2798dfc11533SChris Williamson zcp_check(zhp, prop, NULL, str); 2799e7437265Sahrens break; 2800e7437265Sahrens 280191ebeef5Sahrens case PROP_TYPE_INDEX: 2802fe192f76Sahrens if (get_numeric_property(zhp, prop, src, 2803fe192f76Sahrens &source, &val) != 0) 2804fe192f76Sahrens return (-1); 2805fe192f76Sahrens if (zfs_prop_index_to_string(prop, val, &strval) != 0) 2806e7437265Sahrens return (-1); 2807dfc11533SChris Williamson 2808e7437265Sahrens (void) strlcpy(propbuf, strval, proplen); 2809dfc11533SChris Williamson zcp_check(zhp, prop, NULL, strval); 2810e7437265Sahrens break; 2811e7437265Sahrens 2812e7437265Sahrens default: 2813e7437265Sahrens abort(); 2814e7437265Sahrens } 2815fa9e4066Sahrens } 2816fa9e4066Sahrens 2817fa9e4066Sahrens get_source(zhp, src, source, statbuf, statlen); 2818fa9e4066Sahrens 2819fa9e4066Sahrens return (0); 2820fa9e4066Sahrens } 2821fa9e4066Sahrens 2822fa9e4066Sahrens /* 2823fa9e4066Sahrens * Utility function to get the given numeric property. Does no validation that 2824fa9e4066Sahrens * the given property is the appropriate type; should only be used with 2825fa9e4066Sahrens * hard-coded property types. 2826fa9e4066Sahrens */ 2827fa9e4066Sahrens uint64_t 2828fa9e4066Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop) 2829fa9e4066Sahrens { 2830fa9e4066Sahrens char *source; 283199653d4eSeschrock uint64_t val; 283299653d4eSeschrock 28333cb34c60Sahrens (void) get_numeric_property(zhp, prop, NULL, &source, &val); 2834fa9e4066Sahrens 283599653d4eSeschrock return (val); 2836fa9e4066Sahrens } 2837fa9e4066Sahrens 28387b97dc1aSrm int 28397b97dc1aSrm zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val) 28407b97dc1aSrm { 28417b97dc1aSrm char buf[64]; 28427b97dc1aSrm 284314843421SMatthew Ahrens (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val); 28447b97dc1aSrm return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf)); 28457b97dc1aSrm } 28467b97dc1aSrm 2847fa9e4066Sahrens /* 2848fa9e4066Sahrens * Similar to zfs_prop_get(), but returns the value as an integer. 2849fa9e4066Sahrens */ 2850fa9e4066Sahrens int 2851fa9e4066Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value, 2852990b4856Slling zprop_source_t *src, char *statbuf, size_t statlen) 2853fa9e4066Sahrens { 2854fa9e4066Sahrens char *source; 2855fa9e4066Sahrens 2856fa9e4066Sahrens /* 2857fa9e4066Sahrens * Check to see if this property applies to our object 2858fa9e4066Sahrens */ 2859e45ce728Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) { 2860ece3d9b3Slling return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE, 286199653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot get property '%s'"), 286299653d4eSeschrock zfs_prop_to_name(prop))); 2863e45ce728Sahrens } 2864fa9e4066Sahrens 2865fa9e4066Sahrens if (src) 2866990b4856Slling *src = ZPROP_SRC_NONE; 2867fa9e4066Sahrens 286899653d4eSeschrock if (get_numeric_property(zhp, prop, src, &source, value) != 0) 286999653d4eSeschrock return (-1); 2870fa9e4066Sahrens 2871fa9e4066Sahrens get_source(zhp, src, source, statbuf, statlen); 2872fa9e4066Sahrens 2873fa9e4066Sahrens return (0); 2874fa9e4066Sahrens } 2875fa9e4066Sahrens 287614843421SMatthew Ahrens static int 287714843421SMatthew Ahrens idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser, 287814843421SMatthew Ahrens char **domainp, idmap_rid_t *ridp) 287914843421SMatthew Ahrens { 288014843421SMatthew Ahrens idmap_get_handle_t *get_hdl = NULL; 288114843421SMatthew Ahrens idmap_stat status; 288214843421SMatthew Ahrens int err = EINVAL; 288314843421SMatthew Ahrens 28841fdeec65Sjoyce mcintosh if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS) 288514843421SMatthew Ahrens goto out; 288614843421SMatthew Ahrens 288714843421SMatthew Ahrens if (isuser) { 288814843421SMatthew Ahrens err = idmap_get_sidbyuid(get_hdl, id, 288914843421SMatthew Ahrens IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status); 289014843421SMatthew Ahrens } else { 289114843421SMatthew Ahrens err = idmap_get_sidbygid(get_hdl, id, 289214843421SMatthew Ahrens IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status); 289314843421SMatthew Ahrens } 289414843421SMatthew Ahrens if (err == IDMAP_SUCCESS && 289514843421SMatthew Ahrens idmap_get_mappings(get_hdl) == IDMAP_SUCCESS && 289614843421SMatthew Ahrens status == IDMAP_SUCCESS) 289714843421SMatthew Ahrens err = 0; 289814843421SMatthew Ahrens else 289914843421SMatthew Ahrens err = EINVAL; 290014843421SMatthew Ahrens out: 290114843421SMatthew Ahrens if (get_hdl) 290214843421SMatthew Ahrens idmap_get_destroy(get_hdl); 290314843421SMatthew Ahrens return (err); 290414843421SMatthew Ahrens } 290514843421SMatthew Ahrens 290614843421SMatthew Ahrens /* 290714843421SMatthew Ahrens * convert the propname into parameters needed by kernel 290814843421SMatthew Ahrens * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829 290914843421SMatthew Ahrens * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789 291014843421SMatthew Ahrens */ 291114843421SMatthew Ahrens static int 291214843421SMatthew Ahrens userquota_propname_decode(const char *propname, boolean_t zoned, 291314843421SMatthew Ahrens zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp) 291414843421SMatthew Ahrens { 291514843421SMatthew Ahrens zfs_userquota_prop_t type; 291614843421SMatthew Ahrens char *cp, *end; 29173b12c289SMatthew Ahrens char *numericsid = NULL; 291814843421SMatthew Ahrens boolean_t isuser; 291914843421SMatthew Ahrens 292014843421SMatthew Ahrens domain[0] = '\0'; 29211ed6b69aSGordon Ross *ridp = 0; 292214843421SMatthew Ahrens /* Figure out the property type ({user|group}{quota|space}) */ 292314843421SMatthew Ahrens for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) { 292414843421SMatthew Ahrens if (strncmp(propname, zfs_userquota_prop_prefixes[type], 292514843421SMatthew Ahrens strlen(zfs_userquota_prop_prefixes[type])) == 0) 292614843421SMatthew Ahrens break; 292714843421SMatthew Ahrens } 292814843421SMatthew Ahrens if (type == ZFS_NUM_USERQUOTA_PROPS) 292914843421SMatthew Ahrens return (EINVAL); 293014843421SMatthew Ahrens *typep = type; 293114843421SMatthew Ahrens 293214843421SMatthew Ahrens isuser = (type == ZFS_PROP_USERQUOTA || 293314843421SMatthew Ahrens type == ZFS_PROP_USERUSED); 293414843421SMatthew Ahrens 293514843421SMatthew Ahrens cp = strchr(propname, '@') + 1; 293614843421SMatthew Ahrens 293714843421SMatthew Ahrens if (strchr(cp, '@')) { 293814843421SMatthew Ahrens /* 293914843421SMatthew Ahrens * It's a SID name (eg "user@domain") that needs to be 29403b12c289SMatthew Ahrens * turned into S-1-domainID-RID. 294114843421SMatthew Ahrens */ 29421ed6b69aSGordon Ross int flag = 0; 29431ed6b69aSGordon Ross idmap_stat stat, map_stat; 29441ed6b69aSGordon Ross uid_t pid; 29451ed6b69aSGordon Ross idmap_rid_t rid; 29461ed6b69aSGordon Ross idmap_get_handle_t *gh = NULL; 29471ed6b69aSGordon Ross 29481ed6b69aSGordon Ross stat = idmap_get_create(&gh); 29491ed6b69aSGordon Ross if (stat != IDMAP_SUCCESS) { 29501ed6b69aSGordon Ross idmap_get_destroy(gh); 29511ed6b69aSGordon Ross return (ENOMEM); 29521ed6b69aSGordon Ross } 295314843421SMatthew Ahrens if (zoned && getzoneid() == GLOBAL_ZONEID) 295414843421SMatthew Ahrens return (ENOENT); 29553b12c289SMatthew Ahrens if (isuser) { 29561ed6b69aSGordon Ross stat = idmap_getuidbywinname(cp, NULL, flag, &pid); 29571ed6b69aSGordon Ross if (stat < 0) 29581ed6b69aSGordon Ross return (ENOENT); 29591ed6b69aSGordon Ross stat = idmap_get_sidbyuid(gh, pid, flag, &numericsid, 29601ed6b69aSGordon Ross &rid, &map_stat); 29613b12c289SMatthew Ahrens } else { 29621ed6b69aSGordon Ross stat = idmap_getgidbywinname(cp, NULL, flag, &pid); 29631ed6b69aSGordon Ross if (stat < 0) 29641ed6b69aSGordon Ross return (ENOENT); 29651ed6b69aSGordon Ross stat = idmap_get_sidbygid(gh, pid, flag, &numericsid, 29661ed6b69aSGordon Ross &rid, &map_stat); 29671ed6b69aSGordon Ross } 29681ed6b69aSGordon Ross if (stat < 0) { 29691ed6b69aSGordon Ross idmap_get_destroy(gh); 29701ed6b69aSGordon Ross return (ENOENT); 29713b12c289SMatthew Ahrens } 29721ed6b69aSGordon Ross stat = idmap_get_mappings(gh); 29731ed6b69aSGordon Ross idmap_get_destroy(gh); 29741ed6b69aSGordon Ross 29751ed6b69aSGordon Ross if (stat < 0) { 297614843421SMatthew Ahrens return (ENOENT); 29773b12c289SMatthew Ahrens } 29783b12c289SMatthew Ahrens if (numericsid == NULL) 297914843421SMatthew Ahrens return (ENOENT); 29803b12c289SMatthew Ahrens cp = numericsid; 29811ed6b69aSGordon Ross *ridp = rid; 29823b12c289SMatthew Ahrens /* will be further decoded below */ 29833b12c289SMatthew Ahrens } 29843b12c289SMatthew Ahrens 29853b12c289SMatthew Ahrens if (strncmp(cp, "S-1-", 4) == 0) { 298614843421SMatthew Ahrens /* It's a numeric SID (eg "S-1-234-567-89") */ 29873b12c289SMatthew Ahrens (void) strlcpy(domain, cp, domainlen); 298814843421SMatthew Ahrens errno = 0; 29891ed6b69aSGordon Ross if (*ridp == 0) { 29901ed6b69aSGordon Ross cp = strrchr(domain, '-'); 29911ed6b69aSGordon Ross *cp = '\0'; 29921ed6b69aSGordon Ross cp++; 29931ed6b69aSGordon Ross *ridp = strtoull(cp, &end, 10); 29941ed6b69aSGordon Ross } else { 29951ed6b69aSGordon Ross end = ""; 29961ed6b69aSGordon Ross } 29973b12c289SMatthew Ahrens if (numericsid) { 29983b12c289SMatthew Ahrens free(numericsid); 29993b12c289SMatthew Ahrens numericsid = NULL; 30003b12c289SMatthew Ahrens } 3001777badbaSMatthew Ahrens if (errno != 0 || *end != '\0') 300214843421SMatthew Ahrens return (EINVAL); 300314843421SMatthew Ahrens } else if (!isdigit(*cp)) { 300414843421SMatthew Ahrens /* 300514843421SMatthew Ahrens * It's a user/group name (eg "user") that needs to be 300614843421SMatthew Ahrens * turned into a uid/gid 300714843421SMatthew Ahrens */ 300814843421SMatthew Ahrens if (zoned && getzoneid() == GLOBAL_ZONEID) 300914843421SMatthew Ahrens return (ENOENT); 301014843421SMatthew Ahrens if (isuser) { 301114843421SMatthew Ahrens struct passwd *pw; 301214843421SMatthew Ahrens pw = getpwnam(cp); 301314843421SMatthew Ahrens if (pw == NULL) 301414843421SMatthew Ahrens return (ENOENT); 301514843421SMatthew Ahrens *ridp = pw->pw_uid; 301614843421SMatthew Ahrens } else { 301714843421SMatthew Ahrens struct group *gr; 301814843421SMatthew Ahrens gr = getgrnam(cp); 301914843421SMatthew Ahrens if (gr == NULL) 302014843421SMatthew Ahrens return (ENOENT); 302114843421SMatthew Ahrens *ridp = gr->gr_gid; 302214843421SMatthew Ahrens } 302314843421SMatthew Ahrens } else { 302414843421SMatthew Ahrens /* It's a user/group ID (eg "12345"). */ 302514843421SMatthew Ahrens uid_t id = strtoul(cp, &end, 10); 302614843421SMatthew Ahrens idmap_rid_t rid; 302714843421SMatthew Ahrens char *mapdomain; 302814843421SMatthew Ahrens 302914843421SMatthew Ahrens if (*end != '\0') 303014843421SMatthew Ahrens return (EINVAL); 303114843421SMatthew Ahrens if (id > MAXUID) { 303214843421SMatthew Ahrens /* It's an ephemeral ID. */ 303314843421SMatthew Ahrens if (idmap_id_to_numeric_domain_rid(id, isuser, 303414843421SMatthew Ahrens &mapdomain, &rid) != 0) 303514843421SMatthew Ahrens return (ENOENT); 30363b12c289SMatthew Ahrens (void) strlcpy(domain, mapdomain, domainlen); 303714843421SMatthew Ahrens *ridp = rid; 303814843421SMatthew Ahrens } else { 303914843421SMatthew Ahrens *ridp = id; 304014843421SMatthew Ahrens } 304114843421SMatthew Ahrens } 304214843421SMatthew Ahrens 30433b12c289SMatthew Ahrens ASSERT3P(numericsid, ==, NULL); 304414843421SMatthew Ahrens return (0); 304514843421SMatthew Ahrens } 304614843421SMatthew Ahrens 3047edea4b55SLin Ling static int 3048edea4b55SLin Ling zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname, 3049edea4b55SLin Ling uint64_t *propvalue, zfs_userquota_prop_t *typep) 305014843421SMatthew Ahrens { 305114843421SMatthew Ahrens int err; 305214843421SMatthew Ahrens zfs_cmd_t zc = { 0 }; 305314843421SMatthew Ahrens 305419b94df9SMatthew Ahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 305514843421SMatthew Ahrens 305614843421SMatthew Ahrens err = userquota_propname_decode(propname, 305714843421SMatthew Ahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED), 3058edea4b55SLin Ling typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid); 3059edea4b55SLin Ling zc.zc_objset_type = *typep; 306014843421SMatthew Ahrens if (err) 306114843421SMatthew Ahrens return (err); 306214843421SMatthew Ahrens 306314843421SMatthew Ahrens err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc); 306414843421SMatthew Ahrens if (err) 306514843421SMatthew Ahrens return (err); 306614843421SMatthew Ahrens 3067edea4b55SLin Ling *propvalue = zc.zc_cookie; 3068edea4b55SLin Ling return (0); 3069edea4b55SLin Ling } 3070edea4b55SLin Ling 3071edea4b55SLin Ling int 3072edea4b55SLin Ling zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname, 3073edea4b55SLin Ling uint64_t *propvalue) 3074edea4b55SLin Ling { 3075edea4b55SLin Ling zfs_userquota_prop_t type; 3076edea4b55SLin Ling 3077edea4b55SLin Ling return (zfs_prop_get_userquota_common(zhp, propname, propvalue, 3078edea4b55SLin Ling &type)); 3079edea4b55SLin Ling } 3080edea4b55SLin Ling 3081edea4b55SLin Ling int 3082edea4b55SLin Ling zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname, 3083edea4b55SLin Ling char *propbuf, int proplen, boolean_t literal) 3084edea4b55SLin Ling { 3085edea4b55SLin Ling int err; 3086edea4b55SLin Ling uint64_t propvalue; 3087edea4b55SLin Ling zfs_userquota_prop_t type; 3088edea4b55SLin Ling 3089edea4b55SLin Ling err = zfs_prop_get_userquota_common(zhp, propname, &propvalue, 3090edea4b55SLin Ling &type); 3091edea4b55SLin Ling 3092edea4b55SLin Ling if (err) 3093edea4b55SLin Ling return (err); 3094edea4b55SLin Ling 309514843421SMatthew Ahrens if (literal) { 3096edea4b55SLin Ling (void) snprintf(propbuf, proplen, "%llu", propvalue); 3097edea4b55SLin Ling } else if (propvalue == 0 && 309814843421SMatthew Ahrens (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) { 309914843421SMatthew Ahrens (void) strlcpy(propbuf, "none", proplen); 310014843421SMatthew Ahrens } else { 3101edea4b55SLin Ling zfs_nicenum(propvalue, propbuf, proplen); 310214843421SMatthew Ahrens } 310314843421SMatthew Ahrens return (0); 310414843421SMatthew Ahrens } 310514843421SMatthew Ahrens 310619b94df9SMatthew Ahrens int 310719b94df9SMatthew Ahrens zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname, 310819b94df9SMatthew Ahrens uint64_t *propvalue) 31098ac09fceSRichard Lowe { 311019b94df9SMatthew Ahrens int err; 311119b94df9SMatthew Ahrens zfs_cmd_t zc = { 0 }; 311219b94df9SMatthew Ahrens const char *snapname; 3113ebedde84SEric Taylor 311419b94df9SMatthew Ahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3115e5351341SMatthew Ahrens 311619b94df9SMatthew Ahrens snapname = strchr(propname, '@') + 1; 311719b94df9SMatthew Ahrens if (strchr(snapname, '@')) { 311819b94df9SMatthew Ahrens (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value)); 311919b94df9SMatthew Ahrens } else { 312019b94df9SMatthew Ahrens /* snapname is the short name, append it to zhp's fsname */ 312119b94df9SMatthew Ahrens char *cp; 3122e5351341SMatthew Ahrens 312319b94df9SMatthew Ahrens (void) strlcpy(zc.zc_value, zhp->zfs_name, 312419b94df9SMatthew Ahrens sizeof (zc.zc_value)); 312519b94df9SMatthew Ahrens cp = strchr(zc.zc_value, '@'); 312619b94df9SMatthew Ahrens if (cp != NULL) 312719b94df9SMatthew Ahrens *cp = '\0'; 312819b94df9SMatthew Ahrens (void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value)); 312919b94df9SMatthew Ahrens (void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value)); 31308ac09fceSRichard Lowe } 313119b94df9SMatthew Ahrens 313219b94df9SMatthew Ahrens err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc); 313319b94df9SMatthew Ahrens if (err) 313419b94df9SMatthew Ahrens return (err); 313519b94df9SMatthew Ahrens 313619b94df9SMatthew Ahrens *propvalue = zc.zc_cookie; 313719b94df9SMatthew Ahrens return (0); 3138ebedde84SEric Taylor } 3139ebedde84SEric Taylor 3140fa9e4066Sahrens int 314119b94df9SMatthew Ahrens zfs_prop_get_written(zfs_handle_t *zhp, const char *propname, 314219b94df9SMatthew Ahrens char *propbuf, int proplen, boolean_t literal) 3143fa9e4066Sahrens { 314419b94df9SMatthew Ahrens int err; 314519b94df9SMatthew Ahrens uint64_t propvalue; 31463cb34c60Sahrens 314719b94df9SMatthew Ahrens err = zfs_prop_get_written_int(zhp, propname, &propvalue); 3148ebedde84SEric Taylor 314919b94df9SMatthew Ahrens if (err) 315019b94df9SMatthew Ahrens return (err); 31518ac09fceSRichard Lowe 315219b94df9SMatthew Ahrens if (literal) { 315319b94df9SMatthew Ahrens (void) snprintf(propbuf, proplen, "%llu", propvalue); 315419b94df9SMatthew Ahrens } else { 315519b94df9SMatthew Ahrens zfs_nicenum(propvalue, propbuf, proplen); 3156fa9e4066Sahrens } 315719b94df9SMatthew Ahrens return (0); 31587f7322feSeschrock } 31597f7322feSeschrock 31607f7322feSeschrock /* 316119b94df9SMatthew Ahrens * Returns the name of the given zfs handle. 31627f7322feSeschrock */ 316319b94df9SMatthew Ahrens const char * 316419b94df9SMatthew Ahrens zfs_get_name(const zfs_handle_t *zhp) 31657f7322feSeschrock { 316619b94df9SMatthew Ahrens return (zhp->zfs_name); 316719b94df9SMatthew Ahrens } 31688ac09fceSRichard Lowe 31698808ac5dSYuri Pankov /* 31708808ac5dSYuri Pankov * Returns the name of the parent pool for the given zfs handle. 31718808ac5dSYuri Pankov */ 31728808ac5dSYuri Pankov const char * 31738808ac5dSYuri Pankov zfs_get_pool_name(const zfs_handle_t *zhp) 31748808ac5dSYuri Pankov { 31758808ac5dSYuri Pankov return (zhp->zpool_hdl->zpool_name); 31768808ac5dSYuri Pankov } 31778808ac5dSYuri Pankov 317819b94df9SMatthew Ahrens /* 317919b94df9SMatthew Ahrens * Returns the type of the given zfs handle. 318019b94df9SMatthew Ahrens */ 318119b94df9SMatthew Ahrens zfs_type_t 318219b94df9SMatthew Ahrens zfs_get_type(const zfs_handle_t *zhp) 318319b94df9SMatthew Ahrens { 318419b94df9SMatthew Ahrens return (zhp->zfs_type); 31857f7322feSeschrock } 31867f7322feSeschrock 3187d41c4376SMark J Musante /* 3188d41c4376SMark J Musante * Is one dataset name a child dataset of another? 3189d41c4376SMark J Musante * 3190d41c4376SMark J Musante * Needs to handle these cases: 3191d41c4376SMark J Musante * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo" 3192d41c4376SMark J Musante * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar" 3193d41c4376SMark J Musante * Descendant? No. No. No. Yes. 3194d41c4376SMark J Musante */ 3195d41c4376SMark J Musante static boolean_t 3196d41c4376SMark J Musante is_descendant(const char *ds1, const char *ds2) 3197d41c4376SMark J Musante { 3198d41c4376SMark J Musante size_t d1len = strlen(ds1); 3199d41c4376SMark J Musante 3200d41c4376SMark J Musante /* ds2 can't be a descendant if it's smaller */ 3201d41c4376SMark J Musante if (strlen(ds2) < d1len) 3202d41c4376SMark J Musante return (B_FALSE); 3203d41c4376SMark J Musante 3204d41c4376SMark J Musante /* otherwise, compare strings and verify that there's a '/' char */ 3205d41c4376SMark J Musante return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0)); 3206d41c4376SMark J Musante } 3207d41c4376SMark J Musante 3208fa9e4066Sahrens /* 3209fa9e4066Sahrens * Given a complete name, return just the portion that refers to the parent. 321019b94df9SMatthew Ahrens * Will return -1 if there is no parent (path is just the name of the 321119b94df9SMatthew Ahrens * pool). 3212fa9e4066Sahrens */ 3213fa9e4066Sahrens static int 3214fa9e4066Sahrens parent_name(const char *path, char *buf, size_t buflen) 3215fa9e4066Sahrens { 321619b94df9SMatthew Ahrens char *slashp; 3217fa9e4066Sahrens 321819b94df9SMatthew Ahrens (void) strlcpy(buf, path, buflen); 3219fa9e4066Sahrens 322019b94df9SMatthew Ahrens if ((slashp = strrchr(buf, '/')) == NULL) 322119b94df9SMatthew Ahrens return (-1); 322219b94df9SMatthew Ahrens *slashp = '\0'; 3223fa9e4066Sahrens 3224fa9e4066Sahrens return (0); 3225fa9e4066Sahrens } 3226fa9e4066Sahrens 3227fa9e4066Sahrens /* 32287f1f55eaSvb * If accept_ancestor is false, then check to make sure that the given path has 32297f1f55eaSvb * a parent, and that it exists. If accept_ancestor is true, then find the 32307f1f55eaSvb * closest existing ancestor for the given path. In prefixlen return the 32317f1f55eaSvb * length of already existing prefix of the given path. We also fetch the 32327f1f55eaSvb * 'zoned' property, which is used to validate property settings when creating 32337f1f55eaSvb * new datasets. 3234fa9e4066Sahrens */ 3235fa9e4066Sahrens static int 32367f1f55eaSvb check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned, 32377f1f55eaSvb boolean_t accept_ancestor, int *prefixlen) 3238fa9e4066Sahrens { 3239fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 32409adfa60dSMatthew Ahrens char parent[ZFS_MAX_DATASET_NAME_LEN]; 3241fa9e4066Sahrens char *slash; 32427f7322feSeschrock zfs_handle_t *zhp; 324399653d4eSeschrock char errbuf[1024]; 3244d41c4376SMark J Musante uint64_t is_zoned; 324599653d4eSeschrock 3246deb8317bSMark J Musante (void) snprintf(errbuf, sizeof (errbuf), 3247deb8317bSMark J Musante dgettext(TEXT_DOMAIN, "cannot create '%s'"), path); 3248fa9e4066Sahrens 3249fa9e4066Sahrens /* get parent, and check to see if this is just a pool */ 3250fa9e4066Sahrens if (parent_name(path, parent, sizeof (parent)) != 0) { 325199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 325299653d4eSeschrock "missing dataset name")); 325399653d4eSeschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3254fa9e4066Sahrens } 3255fa9e4066Sahrens 3256fa9e4066Sahrens /* check to see if the pool exists */ 3257fa9e4066Sahrens if ((slash = strchr(parent, '/')) == NULL) 3258fa9e4066Sahrens slash = parent + strlen(parent); 32598ac09fceSRichard Lowe (void) strncpy(zc.zc_name, parent, slash - parent); 3260fa9e4066Sahrens zc.zc_name[slash - parent] = '\0'; 326199653d4eSeschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 && 3262fa9e4066Sahrens errno == ENOENT) { 326399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 326499653d4eSeschrock "no such pool '%s'"), zc.zc_name); 326599653d4eSeschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 3266fa9e4066Sahrens } 3267fa9e4066Sahrens 3268fa9e4066Sahrens /* check to see if the parent dataset exists */ 32697f1f55eaSvb while ((zhp = make_dataset_handle(hdl, parent)) == NULL) { 32707f1f55eaSvb if (errno == ENOENT && accept_ancestor) { 32717f1f55eaSvb /* 32727f1f55eaSvb * Go deeper to find an ancestor, give up on top level. 32737f1f55eaSvb */ 32747f1f55eaSvb if (parent_name(parent, parent, sizeof (parent)) != 0) { 32757f1f55eaSvb zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 32767f1f55eaSvb "no such pool '%s'"), zc.zc_name); 32777f1f55eaSvb return (zfs_error(hdl, EZFS_NOENT, errbuf)); 32787f1f55eaSvb } 32797f1f55eaSvb } else if (errno == ENOENT) { 328099653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 328199653d4eSeschrock "parent does not exist")); 328299653d4eSeschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 32837f1f55eaSvb } else 328499653d4eSeschrock return (zfs_standard_error(hdl, errno, errbuf)); 3285fa9e4066Sahrens } 3286fa9e4066Sahrens 3287d41c4376SMark J Musante is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); 3288d41c4376SMark J Musante if (zoned != NULL) 3289d41c4376SMark J Musante *zoned = is_zoned; 3290d41c4376SMark J Musante 3291fa9e4066Sahrens /* we are in a non-global zone, but parent is in the global zone */ 3292d41c4376SMark J Musante if (getzoneid() != GLOBAL_ZONEID && !is_zoned) { 329399653d4eSeschrock (void) zfs_standard_error(hdl, EPERM, errbuf); 32947f7322feSeschrock zfs_close(zhp); 3295fa9e4066Sahrens return (-1); 3296fa9e4066Sahrens } 3297fa9e4066Sahrens 3298fa9e4066Sahrens /* make sure parent is a filesystem */ 32997f7322feSeschrock if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) { 330099653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 330199653d4eSeschrock "parent is not a filesystem")); 330299653d4eSeschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 33037f7322feSeschrock zfs_close(zhp); 3304fa9e4066Sahrens return (-1); 3305fa9e4066Sahrens } 3306fa9e4066Sahrens 33077f7322feSeschrock zfs_close(zhp); 33087f1f55eaSvb if (prefixlen != NULL) 33097f1f55eaSvb *prefixlen = strlen(parent); 33107f1f55eaSvb return (0); 33117f1f55eaSvb } 33127f1f55eaSvb 33137f1f55eaSvb /* 33147f1f55eaSvb * Finds whether the dataset of the given type(s) exists. 33157f1f55eaSvb */ 33167f1f55eaSvb boolean_t 33177f1f55eaSvb zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types) 33187f1f55eaSvb { 33197f1f55eaSvb zfs_handle_t *zhp; 33207f1f55eaSvb 3321f18faf3fSek if (!zfs_validate_name(hdl, path, types, B_FALSE)) 33227f1f55eaSvb return (B_FALSE); 33237f1f55eaSvb 33247f1f55eaSvb /* 33257f1f55eaSvb * Try to get stats for the dataset, which will tell us if it exists. 33267f1f55eaSvb */ 33277f1f55eaSvb if ((zhp = make_dataset_handle(hdl, path)) != NULL) { 33287f1f55eaSvb int ds_type = zhp->zfs_type; 33297f1f55eaSvb 33307f1f55eaSvb zfs_close(zhp); 33317f1f55eaSvb if (types & ds_type) 33327f1f55eaSvb return (B_TRUE); 33337f1f55eaSvb } 33347f1f55eaSvb return (B_FALSE); 33357f1f55eaSvb } 33367f1f55eaSvb 33373cb34c60Sahrens /* 33383cb34c60Sahrens * Given a path to 'target', create all the ancestors between 33393cb34c60Sahrens * the prefixlen portion of the path, and the target itself. 33403cb34c60Sahrens * Fail if the initial prefixlen-ancestor does not already exist. 33413cb34c60Sahrens */ 33423cb34c60Sahrens int 33433cb34c60Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen) 33443cb34c60Sahrens { 33453cb34c60Sahrens zfs_handle_t *h; 33463cb34c60Sahrens char *cp; 33473cb34c60Sahrens const char *opname; 33483cb34c60Sahrens 33493cb34c60Sahrens /* make sure prefix exists */ 33503cb34c60Sahrens cp = target + prefixlen; 33513cb34c60Sahrens if (*cp != '/') { 33523cb34c60Sahrens assert(strchr(cp, '/') == NULL); 33533cb34c60Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 33543cb34c60Sahrens } else { 33553cb34c60Sahrens *cp = '\0'; 33563cb34c60Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 33573cb34c60Sahrens *cp = '/'; 33583cb34c60Sahrens } 33593cb34c60Sahrens if (h == NULL) 33603cb34c60Sahrens return (-1); 33613cb34c60Sahrens zfs_close(h); 33623cb34c60Sahrens 33633cb34c60Sahrens /* 33643cb34c60Sahrens * Attempt to create, mount, and share any ancestor filesystems, 33653cb34c60Sahrens * up to the prefixlen-long one. 33663cb34c60Sahrens */ 33673cb34c60Sahrens for (cp = target + prefixlen + 1; 336888f61deeSIgor Kozhukhov (cp = strchr(cp, '/')) != NULL; *cp = '/', cp++) { 33693cb34c60Sahrens 33703cb34c60Sahrens *cp = '\0'; 33713cb34c60Sahrens 33723cb34c60Sahrens h = make_dataset_handle(hdl, target); 33733cb34c60Sahrens if (h) { 33743cb34c60Sahrens /* it already exists, nothing to do here */ 33753cb34c60Sahrens zfs_close(h); 33763cb34c60Sahrens continue; 33773cb34c60Sahrens } 33783cb34c60Sahrens 33793cb34c60Sahrens if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM, 33803cb34c60Sahrens NULL) != 0) { 33813cb34c60Sahrens opname = dgettext(TEXT_DOMAIN, "create"); 33823cb34c60Sahrens goto ancestorerr; 33833cb34c60Sahrens } 33843cb34c60Sahrens 33853cb34c60Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 33863cb34c60Sahrens if (h == NULL) { 33873cb34c60Sahrens opname = dgettext(TEXT_DOMAIN, "open"); 33883cb34c60Sahrens goto ancestorerr; 33893cb34c60Sahrens } 33903cb34c60Sahrens 33913cb34c60Sahrens if (zfs_mount(h, NULL, 0) != 0) { 33923cb34c60Sahrens opname = dgettext(TEXT_DOMAIN, "mount"); 33933cb34c60Sahrens goto ancestorerr; 33943cb34c60Sahrens } 33953cb34c60Sahrens 33963cb34c60Sahrens if (zfs_share(h) != 0) { 33973cb34c60Sahrens opname = dgettext(TEXT_DOMAIN, "share"); 33983cb34c60Sahrens goto ancestorerr; 33993cb34c60Sahrens } 34003cb34c60Sahrens 34013cb34c60Sahrens zfs_close(h); 34023cb34c60Sahrens } 34033cb34c60Sahrens 34043cb34c60Sahrens return (0); 34053cb34c60Sahrens 34063cb34c60Sahrens ancestorerr: 34073cb34c60Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 34083cb34c60Sahrens "failed to %s ancestor '%s'"), opname, target); 34093cb34c60Sahrens return (-1); 34103cb34c60Sahrens } 34113cb34c60Sahrens 34127f1f55eaSvb /* 34137f1f55eaSvb * Creates non-existing ancestors of the given path. 34147f1f55eaSvb */ 34157f1f55eaSvb int 34167f1f55eaSvb zfs_create_ancestors(libzfs_handle_t *hdl, const char *path) 34177f1f55eaSvb { 34187f1f55eaSvb int prefix; 34197f1f55eaSvb char *path_copy; 34205ac95da7SSerapheim Dimitropoulos char errbuf[1024]; 3421f83b46baSPaul Dagnelie int rc = 0; 34227f1f55eaSvb 34235ac95da7SSerapheim Dimitropoulos (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 34245ac95da7SSerapheim Dimitropoulos "cannot create '%s'"), path); 34255ac95da7SSerapheim Dimitropoulos 34265ac95da7SSerapheim Dimitropoulos /* 34275ac95da7SSerapheim Dimitropoulos * Check that we are not passing the nesting limit 34285ac95da7SSerapheim Dimitropoulos * before we start creating any ancestors. 34295ac95da7SSerapheim Dimitropoulos */ 34305ac95da7SSerapheim Dimitropoulos if (dataset_nestcheck(path) != 0) { 34315ac95da7SSerapheim Dimitropoulos zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 34325ac95da7SSerapheim Dimitropoulos "maximum name nesting depth exceeded")); 34335ac95da7SSerapheim Dimitropoulos return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 34345ac95da7SSerapheim Dimitropoulos } 34355ac95da7SSerapheim Dimitropoulos 3436d41c4376SMark J Musante if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0) 34377f1f55eaSvb return (-1); 34387f1f55eaSvb 34397f1f55eaSvb if ((path_copy = strdup(path)) != NULL) { 34407f1f55eaSvb rc = create_parents(hdl, path_copy, prefix); 34417f1f55eaSvb free(path_copy); 34427f1f55eaSvb } 34437f1f55eaSvb if (path_copy == NULL || rc != 0) 34447f1f55eaSvb return (-1); 34457f1f55eaSvb 3446fa9e4066Sahrens return (0); 3447fa9e4066Sahrens } 3448fa9e4066Sahrens 3449fa9e4066Sahrens /* 3450e9dbad6fSeschrock * Create a new filesystem or volume. 3451fa9e4066Sahrens */ 3452fa9e4066Sahrens int 345399653d4eSeschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type, 3454e9dbad6fSeschrock nvlist_t *props) 3455fa9e4066Sahrens { 3456fa9e4066Sahrens int ret; 3457fa9e4066Sahrens uint64_t size = 0; 3458fa9e4066Sahrens uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 345999653d4eSeschrock char errbuf[1024]; 3460e9dbad6fSeschrock uint64_t zoned; 346126455f9eSAndriy Gapon enum lzc_dataset_type ost; 3462690031d3Sloli zpool_handle_t *zpool_handle; 346399653d4eSeschrock 346499653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 346599653d4eSeschrock "cannot create '%s'"), path); 3466fa9e4066Sahrens 3467fa9e4066Sahrens /* validate the path, taking care to note the extended error message */ 3468f18faf3fSek if (!zfs_validate_name(hdl, path, type, B_TRUE)) 346999653d4eSeschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3470fa9e4066Sahrens 34715ac95da7SSerapheim Dimitropoulos if (dataset_nestcheck(path) != 0) { 34725ac95da7SSerapheim Dimitropoulos zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 34735ac95da7SSerapheim Dimitropoulos "maximum name nesting depth exceeded")); 34745ac95da7SSerapheim Dimitropoulos return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 34755ac95da7SSerapheim Dimitropoulos } 34765ac95da7SSerapheim Dimitropoulos 3477fa9e4066Sahrens /* validate parents exist */ 34787f1f55eaSvb if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0) 3479fa9e4066Sahrens return (-1); 3480fa9e4066Sahrens 3481fa9e4066Sahrens /* 3482fa9e4066Sahrens * The failure modes when creating a dataset of a different type over 3483fa9e4066Sahrens * one that already exists is a little strange. In particular, if you 3484fa9e4066Sahrens * try to create a dataset on top of an existing dataset, the ioctl() 3485fa9e4066Sahrens * will return ENOENT, not EEXIST. To prevent this from happening, we 3486fa9e4066Sahrens * first try to see if the dataset exists. 3487fa9e4066Sahrens */ 34884445fffbSMatthew Ahrens if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) { 348999653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 349099653d4eSeschrock "dataset already exists")); 349199653d4eSeschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3492fa9e4066Sahrens } 3493fa9e4066Sahrens 3494fa9e4066Sahrens if (type == ZFS_TYPE_VOLUME) 349526455f9eSAndriy Gapon ost = LZC_DATSET_TYPE_ZVOL; 3496fa9e4066Sahrens else 349726455f9eSAndriy Gapon ost = LZC_DATSET_TYPE_ZFS; 3498fa9e4066Sahrens 3499e9316f76SJoe Stein /* open zpool handle for prop validation */ 35009adfa60dSMatthew Ahrens char pool_path[ZFS_MAX_DATASET_NAME_LEN]; 3501e9316f76SJoe Stein (void) strlcpy(pool_path, path, sizeof (pool_path)); 3502e9316f76SJoe Stein 3503e9316f76SJoe Stein /* truncate pool_path at first slash */ 3504e9316f76SJoe Stein char *p = strchr(pool_path, '/'); 3505e9316f76SJoe Stein if (p != NULL) 3506e9316f76SJoe Stein *p = '\0'; 3507e9316f76SJoe Stein 3508690031d3Sloli if ((zpool_handle = zpool_open(hdl, pool_path)) == NULL) 3509690031d3Sloli return (-1); 3510e9316f76SJoe Stein 35110a48a24eStimh if (props && (props = zfs_valid_proplist(hdl, type, props, 3512e9316f76SJoe Stein zoned, NULL, zpool_handle, errbuf)) == 0) { 3513e9316f76SJoe Stein zpool_close(zpool_handle); 3514e9dbad6fSeschrock return (-1); 3515e9316f76SJoe Stein } 3516e9316f76SJoe Stein zpool_close(zpool_handle); 3517e9dbad6fSeschrock 3518fa9e4066Sahrens if (type == ZFS_TYPE_VOLUME) { 35195c5460e9Seschrock /* 35205c5460e9Seschrock * If we are creating a volume, the size and block size must 35215c5460e9Seschrock * satisfy a few restraints. First, the blocksize must be a 35225c5460e9Seschrock * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the 35235c5460e9Seschrock * volsize must be a multiple of the block size, and cannot be 35245c5460e9Seschrock * zero. 35255c5460e9Seschrock */ 3526e9dbad6fSeschrock if (props == NULL || nvlist_lookup_uint64(props, 3527e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) { 3528e9dbad6fSeschrock nvlist_free(props); 352999653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3530e9dbad6fSeschrock "missing volume size")); 3531e9dbad6fSeschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 3532e9dbad6fSeschrock } 3533e9dbad6fSeschrock 3534e9dbad6fSeschrock if ((ret = nvlist_lookup_uint64(props, 3535e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 3536e9dbad6fSeschrock &blocksize)) != 0) { 3537e9dbad6fSeschrock if (ret == ENOENT) { 3538e9dbad6fSeschrock blocksize = zfs_prop_default_numeric( 3539e9dbad6fSeschrock ZFS_PROP_VOLBLOCKSIZE); 3540e9dbad6fSeschrock } else { 3541e9dbad6fSeschrock nvlist_free(props); 3542e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3543e9dbad6fSeschrock "missing volume block size")); 3544e9dbad6fSeschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 3545e9dbad6fSeschrock } 3546fa9e4066Sahrens } 3547fa9e4066Sahrens 3548e9dbad6fSeschrock if (size == 0) { 3549e9dbad6fSeschrock nvlist_free(props); 355099653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3551e9dbad6fSeschrock "volume size cannot be zero")); 3552e9dbad6fSeschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 35535c5460e9Seschrock } 35545c5460e9Seschrock 35555c5460e9Seschrock if (size % blocksize != 0) { 3556e9dbad6fSeschrock nvlist_free(props); 355799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3558e9dbad6fSeschrock "volume size must be a multiple of volume block " 3559e9dbad6fSeschrock "size")); 3560e9dbad6fSeschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 35615c5460e9Seschrock } 3562fa9e4066Sahrens } 3563fa9e4066Sahrens 3564fa9e4066Sahrens /* create the dataset */ 35654445fffbSMatthew Ahrens ret = lzc_create(path, ost, props); 35664445fffbSMatthew Ahrens nvlist_free(props); 3567e9dbad6fSeschrock 3568fa9e4066Sahrens /* check for failure */ 3569fa9e4066Sahrens if (ret != 0) { 35709adfa60dSMatthew Ahrens char parent[ZFS_MAX_DATASET_NAME_LEN]; 3571fa9e4066Sahrens (void) parent_name(path, parent, sizeof (parent)); 3572fa9e4066Sahrens 3573fa9e4066Sahrens switch (errno) { 3574fa9e4066Sahrens case ENOENT: 357599653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 357699653d4eSeschrock "no such parent '%s'"), parent); 357799653d4eSeschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 3578fa9e4066Sahrens 3579fa9e4066Sahrens case EINVAL: 358099653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3581d7d4af51Smmusante "parent '%s' is not a filesystem"), parent); 358299653d4eSeschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 3583fa9e4066Sahrens 358440feaa91Sahrens case ENOTSUP: 358540feaa91Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 358640feaa91Sahrens "pool must be upgraded to set this " 358740feaa91Sahrens "property or value")); 358840feaa91Sahrens return (zfs_error(hdl, EZFS_BADVERSION, errbuf)); 35899fa2266dSYuri Pankov case ERANGE: 35909fa2266dSYuri Pankov zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35919fa2266dSYuri Pankov "invalid property value(s) specified")); 35929fa2266dSYuri Pankov return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 3593fa9e4066Sahrens #ifdef _ILP32 3594fa9e4066Sahrens case EOVERFLOW: 3595fa9e4066Sahrens /* 3596fa9e4066Sahrens * This platform can't address a volume this big. 3597fa9e4066Sahrens */ 359899653d4eSeschrock if (type == ZFS_TYPE_VOLUME) 359999653d4eSeschrock return (zfs_error(hdl, EZFS_VOLTOOBIG, 360099653d4eSeschrock errbuf)); 3601fa9e4066Sahrens #endif 360299653d4eSeschrock /* FALLTHROUGH */ 3603fa9e4066Sahrens default: 360499653d4eSeschrock return (zfs_standard_error(hdl, errno, errbuf)); 3605fa9e4066Sahrens } 3606fa9e4066Sahrens } 3607fa9e4066Sahrens 3608fa9e4066Sahrens return (0); 3609fa9e4066Sahrens } 3610fa9e4066Sahrens 3611fa9e4066Sahrens /* 3612fa9e4066Sahrens * Destroys the given dataset. The caller must make sure that the filesystem 361365fec9f6SChristopher Siden * isn't mounted, and that there are no active dependents. If the file system 361465fec9f6SChristopher Siden * does not exist this function does nothing. 3615fa9e4066Sahrens */ 3616fa9e4066Sahrens int 3617842727c2SChris Kirby zfs_destroy(zfs_handle_t *zhp, boolean_t defer) 3618fa9e4066Sahrens { 3619049ba636SAndriy Gapon int error; 3620049ba636SAndriy Gapon 3621049ba636SAndriy Gapon if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT && defer) 3622049ba636SAndriy Gapon return (EINVAL); 3623fa9e4066Sahrens 362478f17100SMatthew Ahrens if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) { 362578f17100SMatthew Ahrens nvlist_t *nv = fnvlist_alloc(); 362678f17100SMatthew Ahrens fnvlist_add_boolean(nv, zhp->zfs_name); 3627049ba636SAndriy Gapon error = lzc_destroy_bookmarks(nv, NULL); 362878f17100SMatthew Ahrens fnvlist_free(nv); 362978f17100SMatthew Ahrens if (error != 0) { 3630049ba636SAndriy Gapon return (zfs_standard_error_fmt(zhp->zfs_hdl, error, 363178f17100SMatthew Ahrens dgettext(TEXT_DOMAIN, "cannot destroy '%s'"), 363278f17100SMatthew Ahrens zhp->zfs_name)); 363378f17100SMatthew Ahrens } 363478f17100SMatthew Ahrens return (0); 363578f17100SMatthew Ahrens } 363678f17100SMatthew Ahrens 3637049ba636SAndriy Gapon if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 3638049ba636SAndriy Gapon nvlist_t *nv = fnvlist_alloc(); 3639049ba636SAndriy Gapon fnvlist_add_boolean(nv, zhp->zfs_name); 3640049ba636SAndriy Gapon error = lzc_destroy_snaps(nv, defer, NULL); 3641049ba636SAndriy Gapon fnvlist_free(nv); 3642fa9e4066Sahrens } else { 3643049ba636SAndriy Gapon error = lzc_destroy(zhp->zfs_name); 3644fa9e4066Sahrens } 3645fa9e4066Sahrens 3646049ba636SAndriy Gapon if (error != 0 && error != ENOENT) { 3647ece3d9b3Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, errno, 364899653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot destroy '%s'"), 364999653d4eSeschrock zhp->zfs_name)); 36501d452cf5Sahrens } 3651fa9e4066Sahrens 3652fa9e4066Sahrens remove_mountpoint(zhp); 3653fa9e4066Sahrens 3654fa9e4066Sahrens return (0); 3655fa9e4066Sahrens } 3656fa9e4066Sahrens 36571d452cf5Sahrens struct destroydata { 365819b94df9SMatthew Ahrens nvlist_t *nvl; 365919b94df9SMatthew Ahrens const char *snapname; 36601d452cf5Sahrens }; 36611d452cf5Sahrens 36621d452cf5Sahrens static int 3663681d9761SEric Taylor zfs_check_snap_cb(zfs_handle_t *zhp, void *arg) 36641d452cf5Sahrens { 36651d452cf5Sahrens struct destroydata *dd = arg; 36669adfa60dSMatthew Ahrens char name[ZFS_MAX_DATASET_NAME_LEN]; 3667681d9761SEric Taylor int rv = 0; 36681d452cf5Sahrens 366919b94df9SMatthew Ahrens (void) snprintf(name, sizeof (name), 367019b94df9SMatthew Ahrens "%s@%s", zhp->zfs_name, dd->snapname); 36711d452cf5Sahrens 3672a7a845e4SSteven Hartland if (lzc_exists(name)) 367319b94df9SMatthew Ahrens verify(nvlist_add_boolean(dd->nvl, name) == 0); 36741d452cf5Sahrens 367519b94df9SMatthew Ahrens rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd); 367619b94df9SMatthew Ahrens zfs_close(zhp); 36773ccfa83cSahrens return (rv); 36781d452cf5Sahrens } 36791d452cf5Sahrens 36801d452cf5Sahrens /* 36811d452cf5Sahrens * Destroys all snapshots with the given name in zhp & descendants. 36821d452cf5Sahrens */ 36831d452cf5Sahrens int 3684842727c2SChris Kirby zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer) 36851d452cf5Sahrens { 36861d452cf5Sahrens int ret; 36871d452cf5Sahrens struct destroydata dd = { 0 }; 36881d452cf5Sahrens 36891d452cf5Sahrens dd.snapname = snapname; 369019b94df9SMatthew Ahrens verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0); 369119b94df9SMatthew Ahrens (void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd); 36921d452cf5Sahrens 3693a7a845e4SSteven Hartland if (nvlist_empty(dd.nvl)) { 369419b94df9SMatthew Ahrens ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT, 36951d452cf5Sahrens dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"), 369619b94df9SMatthew Ahrens zhp->zfs_name, snapname); 369719b94df9SMatthew Ahrens } else { 36983b2aab18SMatthew Ahrens ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer); 36991d452cf5Sahrens } 370019b94df9SMatthew Ahrens nvlist_free(dd.nvl); 370119b94df9SMatthew Ahrens return (ret); 370219b94df9SMatthew Ahrens } 370319b94df9SMatthew Ahrens 370419b94df9SMatthew Ahrens /* 37053b2aab18SMatthew Ahrens * Destroys all the snapshots named in the nvlist. 370619b94df9SMatthew Ahrens */ 370719b94df9SMatthew Ahrens int 37083b2aab18SMatthew Ahrens zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer) 370919b94df9SMatthew Ahrens { 371019b94df9SMatthew Ahrens int ret; 37114cde22c2SChris Williamson nvlist_t *errlist = NULL; 37121d452cf5Sahrens 37134445fffbSMatthew Ahrens ret = lzc_destroy_snaps(snaps, defer, &errlist); 37141d452cf5Sahrens 37154cde22c2SChris Williamson if (ret == 0) { 37164cde22c2SChris Williamson nvlist_free(errlist); 37173b2aab18SMatthew Ahrens return (0); 37184cde22c2SChris Williamson } 37194445fffbSMatthew Ahrens 3720a7a845e4SSteven Hartland if (nvlist_empty(errlist)) { 37213b2aab18SMatthew Ahrens char errbuf[1024]; 37223b2aab18SMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), 37233b2aab18SMatthew Ahrens dgettext(TEXT_DOMAIN, "cannot destroy snapshots")); 37243b2aab18SMatthew Ahrens 37253b2aab18SMatthew Ahrens ret = zfs_standard_error(hdl, ret, errbuf); 37263b2aab18SMatthew Ahrens } 37273b2aab18SMatthew Ahrens for (nvpair_t *pair = nvlist_next_nvpair(errlist, NULL); 37283b2aab18SMatthew Ahrens pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) { 37293b2aab18SMatthew Ahrens char errbuf[1024]; 37303b2aab18SMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), 37313b2aab18SMatthew Ahrens dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"), 37323b2aab18SMatthew Ahrens nvpair_name(pair)); 37333b2aab18SMatthew Ahrens 37343b2aab18SMatthew Ahrens switch (fnvpair_value_int32(pair)) { 37353b2aab18SMatthew Ahrens case EEXIST: 37363b2aab18SMatthew Ahrens zfs_error_aux(hdl, 37373b2aab18SMatthew Ahrens dgettext(TEXT_DOMAIN, "snapshot is cloned")); 37383b2aab18SMatthew Ahrens ret = zfs_error(hdl, EZFS_EXISTS, errbuf); 37393b2aab18SMatthew Ahrens break; 37403b2aab18SMatthew Ahrens default: 37413b2aab18SMatthew Ahrens ret = zfs_standard_error(hdl, errno, errbuf); 37423b2aab18SMatthew Ahrens break; 37431d452cf5Sahrens } 37441d452cf5Sahrens } 37451d452cf5Sahrens 37464cde22c2SChris Williamson nvlist_free(errlist); 37474445fffbSMatthew Ahrens return (ret); 37481d452cf5Sahrens } 37491d452cf5Sahrens 3750fa9e4066Sahrens /* 3751fa9e4066Sahrens * Clones the given dataset. The target must be of the same type as the source. 3752fa9e4066Sahrens */ 3753fa9e4066Sahrens int 3754e9dbad6fSeschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props) 3755fa9e4066Sahrens { 37569adfa60dSMatthew Ahrens char parent[ZFS_MAX_DATASET_NAME_LEN]; 3757fa9e4066Sahrens int ret; 375899653d4eSeschrock char errbuf[1024]; 375999653d4eSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 3760e9dbad6fSeschrock uint64_t zoned; 3761fa9e4066Sahrens 3762fa9e4066Sahrens assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 3763fa9e4066Sahrens 376499653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 376599653d4eSeschrock "cannot create '%s'"), target); 376699653d4eSeschrock 376719b94df9SMatthew Ahrens /* validate the target/clone name */ 3768f18faf3fSek if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE)) 376999653d4eSeschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3770fa9e4066Sahrens 3771fa9e4066Sahrens /* validate parents exist */ 37727f1f55eaSvb if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0) 3773fa9e4066Sahrens return (-1); 3774fa9e4066Sahrens 3775fa9e4066Sahrens (void) parent_name(target, parent, sizeof (parent)); 3776fa9e4066Sahrens 3777fa9e4066Sahrens /* do the clone */ 3778e9dbad6fSeschrock 3779e9dbad6fSeschrock if (props) { 37804445fffbSMatthew Ahrens zfs_type_t type; 37811c10ae76SMike Gerdts 37824445fffbSMatthew Ahrens if (ZFS_IS_VOLUME(zhp)) { 37834445fffbSMatthew Ahrens type = ZFS_TYPE_VOLUME; 37844445fffbSMatthew Ahrens } else { 37854445fffbSMatthew Ahrens type = ZFS_TYPE_FILESYSTEM; 37864445fffbSMatthew Ahrens } 37870a48a24eStimh if ((props = zfs_valid_proplist(hdl, type, props, zoned, 3788e9316f76SJoe Stein zhp, zhp->zpool_hdl, errbuf)) == NULL) 3789e9dbad6fSeschrock return (-1); 37901c10ae76SMike Gerdts if (zfs_fix_auto_resv(zhp, props) == -1) { 37911c10ae76SMike Gerdts nvlist_free(props); 37921c10ae76SMike Gerdts return (-1); 37931c10ae76SMike Gerdts } 3794e9dbad6fSeschrock } 3795fa9e4066Sahrens 37964445fffbSMatthew Ahrens ret = lzc_clone(target, zhp->zfs_name, props); 37974445fffbSMatthew Ahrens nvlist_free(props); 3798e9dbad6fSeschrock 3799fa9e4066Sahrens if (ret != 0) { 3800fa9e4066Sahrens switch (errno) { 3801fa9e4066Sahrens 3802fa9e4066Sahrens case ENOENT: 3803fa9e4066Sahrens /* 3804fa9e4066Sahrens * The parent doesn't exist. We should have caught this 3805fa9e4066Sahrens * above, but there may a race condition that has since 3806fa9e4066Sahrens * destroyed the parent. 3807fa9e4066Sahrens * 3808fa9e4066Sahrens * At this point, we don't know whether it's the source 3809fa9e4066Sahrens * that doesn't exist anymore, or whether the target 3810fa9e4066Sahrens * dataset doesn't exist. 3811fa9e4066Sahrens */ 381299653d4eSeschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 381399653d4eSeschrock "no such parent '%s'"), parent); 381499653d4eSeschrock return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf)); 3815fa9e4066Sahrens 381699653d4eSeschrock case EXDEV: 381799653d4eSeschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 381899653d4eSeschrock "source and target pools differ")); 381999653d4eSeschrock return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET, 382099653d4eSeschrock errbuf)); 3821fa9e4066Sahrens 382299653d4eSeschrock default: 382399653d4eSeschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 382499653d4eSeschrock errbuf)); 382599653d4eSeschrock } 382699653d4eSeschrock } 3827fa9e4066Sahrens 382899653d4eSeschrock return (ret); 382999653d4eSeschrock } 383099653d4eSeschrock 383199653d4eSeschrock /* 383299653d4eSeschrock * Promotes the given clone fs to be the clone parent. 383399653d4eSeschrock */ 383499653d4eSeschrock int 383599653d4eSeschrock zfs_promote(zfs_handle_t *zhp) 383699653d4eSeschrock { 383799653d4eSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 3838a4b8c9aaSAndrew Stormont char snapname[ZFS_MAX_DATASET_NAME_LEN]; 383999653d4eSeschrock int ret; 384099653d4eSeschrock char errbuf[1024]; 384199653d4eSeschrock 384299653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 384399653d4eSeschrock "cannot promote '%s'"), zhp->zfs_name); 384499653d4eSeschrock 384599653d4eSeschrock if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 384699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 384799653d4eSeschrock "snapshots can not be promoted")); 384899653d4eSeschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 384999653d4eSeschrock } 385099653d4eSeschrock 3851a4b8c9aaSAndrew Stormont if (zhp->zfs_dmustats.dds_origin[0] == '\0') { 385299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 385399653d4eSeschrock "not a cloned filesystem")); 385499653d4eSeschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 385599653d4eSeschrock } 385699653d4eSeschrock 3857add927f8Sloli if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE)) 3858add927f8Sloli return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3859add927f8Sloli 3860a4b8c9aaSAndrew Stormont ret = lzc_promote(zhp->zfs_name, snapname, sizeof (snapname)); 386199653d4eSeschrock 386299653d4eSeschrock if (ret != 0) { 3863a4b8c9aaSAndrew Stormont switch (ret) { 386499653d4eSeschrock case EEXIST: 3865681d9761SEric Taylor /* There is a conflicting snapshot name. */ 386699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3867681d9761SEric Taylor "conflicting snapshot '%s' from parent '%s'"), 3868a4b8c9aaSAndrew Stormont snapname, zhp->zfs_dmustats.dds_origin); 386999653d4eSeschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3870fa9e4066Sahrens 3871fa9e4066Sahrens default: 3872a4b8c9aaSAndrew Stormont return (zfs_standard_error(hdl, ret, errbuf)); 3873fa9e4066Sahrens } 3874fa9e4066Sahrens } 3875e9dbad6fSeschrock return (ret); 38761d452cf5Sahrens } 38771d452cf5Sahrens 38784445fffbSMatthew Ahrens typedef struct snapdata { 38794445fffbSMatthew Ahrens nvlist_t *sd_nvl; 38804445fffbSMatthew Ahrens const char *sd_snapname; 38814445fffbSMatthew Ahrens } snapdata_t; 38824445fffbSMatthew Ahrens 38834445fffbSMatthew Ahrens static int 38844445fffbSMatthew Ahrens zfs_snapshot_cb(zfs_handle_t *zhp, void *arg) 38854445fffbSMatthew Ahrens { 38864445fffbSMatthew Ahrens snapdata_t *sd = arg; 38879adfa60dSMatthew Ahrens char name[ZFS_MAX_DATASET_NAME_LEN]; 38884445fffbSMatthew Ahrens int rv = 0; 38894445fffbSMatthew Ahrens 3890ca48f36fSKeith M Wesolowski if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) { 3891ca48f36fSKeith M Wesolowski (void) snprintf(name, sizeof (name), 3892ca48f36fSKeith M Wesolowski "%s@%s", zfs_get_name(zhp), sd->sd_snapname); 38934445fffbSMatthew Ahrens 3894ca48f36fSKeith M Wesolowski fnvlist_add_boolean(sd->sd_nvl, name); 38954445fffbSMatthew Ahrens 3896ca48f36fSKeith M Wesolowski rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd); 3897ca48f36fSKeith M Wesolowski } 38984445fffbSMatthew Ahrens zfs_close(zhp); 3899ca48f36fSKeith M Wesolowski 39004445fffbSMatthew Ahrens return (rv); 39014445fffbSMatthew Ahrens } 39024445fffbSMatthew Ahrens 39035cabbc6bSPrashanth Sreenivasa int 39045cabbc6bSPrashanth Sreenivasa zfs_remap_indirects(libzfs_handle_t *hdl, const char *fs) 39055cabbc6bSPrashanth Sreenivasa { 39065cabbc6bSPrashanth Sreenivasa int err; 39075cabbc6bSPrashanth Sreenivasa char errbuf[1024]; 39085cabbc6bSPrashanth Sreenivasa 39095cabbc6bSPrashanth Sreenivasa (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 39100b2e8253Sloli "cannot remap dataset '%s'"), fs); 39115cabbc6bSPrashanth Sreenivasa 39125cabbc6bSPrashanth Sreenivasa err = lzc_remap(fs); 39135cabbc6bSPrashanth Sreenivasa 39145cabbc6bSPrashanth Sreenivasa if (err != 0) { 39150b2e8253Sloli switch (err) { 39160b2e8253Sloli case ENOTSUP: 39170b2e8253Sloli zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 39180b2e8253Sloli "pool must be upgraded")); 39190b2e8253Sloli (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 39200b2e8253Sloli break; 39210b2e8253Sloli case EINVAL: 39220b2e8253Sloli (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 39230b2e8253Sloli break; 39240b2e8253Sloli default: 39250b2e8253Sloli (void) zfs_standard_error(hdl, err, errbuf); 39260b2e8253Sloli break; 39270b2e8253Sloli } 39285cabbc6bSPrashanth Sreenivasa } 39295cabbc6bSPrashanth Sreenivasa 39305cabbc6bSPrashanth Sreenivasa return (err); 39315cabbc6bSPrashanth Sreenivasa } 39325cabbc6bSPrashanth Sreenivasa 3933fa9e4066Sahrens /* 39344445fffbSMatthew Ahrens * Creates snapshots. The keys in the snaps nvlist are the snapshots to be 39354445fffbSMatthew Ahrens * created. 3936fa9e4066Sahrens */ 3937fa9e4066Sahrens int 39384445fffbSMatthew Ahrens zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props) 3939fa9e4066Sahrens { 3940fa9e4066Sahrens int ret; 394199653d4eSeschrock char errbuf[1024]; 39424445fffbSMatthew Ahrens nvpair_t *elem; 39434445fffbSMatthew Ahrens nvlist_t *errors; 3944fa9e4066Sahrens 394599653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 39464445fffbSMatthew Ahrens "cannot create snapshots ")); 394799653d4eSeschrock 39484445fffbSMatthew Ahrens elem = NULL; 39494445fffbSMatthew Ahrens while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) { 39504445fffbSMatthew Ahrens const char *snapname = nvpair_name(elem); 39514445fffbSMatthew Ahrens 39524445fffbSMatthew Ahrens /* validate the target name */ 39534445fffbSMatthew Ahrens if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT, 39544445fffbSMatthew Ahrens B_TRUE)) { 39554445fffbSMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), 39564445fffbSMatthew Ahrens dgettext(TEXT_DOMAIN, 39574445fffbSMatthew Ahrens "cannot create snapshot '%s'"), snapname); 39584445fffbSMatthew Ahrens return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 39594445fffbSMatthew Ahrens } 39604445fffbSMatthew Ahrens } 3961fa9e4066Sahrens 3962e9316f76SJoe Stein /* 3963e9316f76SJoe Stein * get pool handle for prop validation. assumes all snaps are in the 3964e9316f76SJoe Stein * same pool, as does lzc_snapshot (below). 3965e9316f76SJoe Stein */ 39669adfa60dSMatthew Ahrens char pool[ZFS_MAX_DATASET_NAME_LEN]; 3967e9316f76SJoe Stein elem = nvlist_next_nvpair(snaps, NULL); 3968e9316f76SJoe Stein (void) strlcpy(pool, nvpair_name(elem), sizeof (pool)); 3969e9316f76SJoe Stein pool[strcspn(pool, "/@")] = '\0'; 3970e9316f76SJoe Stein zpool_handle_t *zpool_hdl = zpool_open(hdl, pool); 3971e9316f76SJoe Stein 39724445fffbSMatthew Ahrens if (props != NULL && 39734445fffbSMatthew Ahrens (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT, 3974e9316f76SJoe Stein props, B_FALSE, NULL, zpool_hdl, errbuf)) == NULL) { 3975e9316f76SJoe Stein zpool_close(zpool_hdl); 39764445fffbSMatthew Ahrens return (-1); 39774445fffbSMatthew Ahrens } 3978e9316f76SJoe Stein zpool_close(zpool_hdl); 3979bb0ade09Sahrens 39804445fffbSMatthew Ahrens ret = lzc_snapshot(snaps, props, &errors); 39814445fffbSMatthew Ahrens 39824445fffbSMatthew Ahrens if (ret != 0) { 39834445fffbSMatthew Ahrens boolean_t printed = B_FALSE; 39844445fffbSMatthew Ahrens for (elem = nvlist_next_nvpair(errors, NULL); 39854445fffbSMatthew Ahrens elem != NULL; 39864445fffbSMatthew Ahrens elem = nvlist_next_nvpair(errors, elem)) { 39874445fffbSMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), 39884445fffbSMatthew Ahrens dgettext(TEXT_DOMAIN, 39894445fffbSMatthew Ahrens "cannot create snapshot '%s'"), nvpair_name(elem)); 39904445fffbSMatthew Ahrens (void) zfs_standard_error(hdl, 39914445fffbSMatthew Ahrens fnvpair_value_int32(elem), errbuf); 39924445fffbSMatthew Ahrens printed = B_TRUE; 3993bb0ade09Sahrens } 39944445fffbSMatthew Ahrens if (!printed) { 39954445fffbSMatthew Ahrens switch (ret) { 39964445fffbSMatthew Ahrens case EXDEV: 39974445fffbSMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 39984445fffbSMatthew Ahrens "multiple snapshots of same " 39994445fffbSMatthew Ahrens "fs not allowed")); 40004445fffbSMatthew Ahrens (void) zfs_error(hdl, EZFS_EXISTS, errbuf); 4001bb0ade09Sahrens 40024445fffbSMatthew Ahrens break; 40034445fffbSMatthew Ahrens default: 40044445fffbSMatthew Ahrens (void) zfs_standard_error(hdl, ret, errbuf); 40054445fffbSMatthew Ahrens } 40064445fffbSMatthew Ahrens } 4007bb0ade09Sahrens } 4008bb0ade09Sahrens 40094445fffbSMatthew Ahrens nvlist_free(props); 40104445fffbSMatthew Ahrens nvlist_free(errors); 40114445fffbSMatthew Ahrens return (ret); 40124445fffbSMatthew Ahrens } 40134445fffbSMatthew Ahrens 40144445fffbSMatthew Ahrens int 40154445fffbSMatthew Ahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive, 40164445fffbSMatthew Ahrens nvlist_t *props) 40174445fffbSMatthew Ahrens { 40184445fffbSMatthew Ahrens int ret; 40194445fffbSMatthew Ahrens snapdata_t sd = { 0 }; 40209adfa60dSMatthew Ahrens char fsname[ZFS_MAX_DATASET_NAME_LEN]; 40214445fffbSMatthew Ahrens char *cp; 40224445fffbSMatthew Ahrens zfs_handle_t *zhp; 40234445fffbSMatthew Ahrens char errbuf[1024]; 40244445fffbSMatthew Ahrens 40254445fffbSMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 40264445fffbSMatthew Ahrens "cannot snapshot %s"), path); 40274445fffbSMatthew Ahrens 40284445fffbSMatthew Ahrens if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE)) 40294445fffbSMatthew Ahrens return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 40304445fffbSMatthew Ahrens 40314445fffbSMatthew Ahrens (void) strlcpy(fsname, path, sizeof (fsname)); 40324445fffbSMatthew Ahrens cp = strchr(fsname, '@'); 40334445fffbSMatthew Ahrens *cp = '\0'; 40344445fffbSMatthew Ahrens sd.sd_snapname = cp + 1; 4035fa9e4066Sahrens 40364445fffbSMatthew Ahrens if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | 4037fa9e4066Sahrens ZFS_TYPE_VOLUME)) == NULL) { 4038fa9e4066Sahrens return (-1); 4039fa9e4066Sahrens } 4040fa9e4066Sahrens 40414445fffbSMatthew Ahrens verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0); 40424445fffbSMatthew Ahrens if (recursive) { 40434445fffbSMatthew Ahrens (void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd); 40444445fffbSMatthew Ahrens } else { 40454445fffbSMatthew Ahrens fnvlist_add_boolean(sd.sd_nvl, path); 4046681d9761SEric Taylor } 4047fa9e4066Sahrens 40484445fffbSMatthew Ahrens ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props); 40494445fffbSMatthew Ahrens nvlist_free(sd.sd_nvl); 4050fa9e4066Sahrens zfs_close(zhp); 4051fa9e4066Sahrens return (ret); 4052fa9e4066Sahrens } 4053fa9e4066Sahrens 4054fa9e4066Sahrens /* 4055b12a1c38Slling * Destroy any more recent snapshots. We invoke this callback on any dependents 4056b12a1c38Slling * of the snapshot first. If the 'cb_dependent' member is non-zero, then this 4057b12a1c38Slling * is a dependent and we should just destroy it without checking the transaction 4058b12a1c38Slling * group. 4059fa9e4066Sahrens */ 4060b12a1c38Slling typedef struct rollback_data { 4061b12a1c38Slling const char *cb_target; /* the snapshot */ 4062b12a1c38Slling uint64_t cb_create; /* creation time reference */ 4063c391e322Sahrens boolean_t cb_error; 4064c391e322Sahrens boolean_t cb_force; 4065b12a1c38Slling } rollback_data_t; 4066b12a1c38Slling 4067b12a1c38Slling static int 406878f17100SMatthew Ahrens rollback_destroy_dependent(zfs_handle_t *zhp, void *data) 4069b12a1c38Slling { 4070b12a1c38Slling rollback_data_t *cbp = data; 407178f17100SMatthew Ahrens prop_changelist_t *clp; 407278f17100SMatthew Ahrens 407378f17100SMatthew Ahrens /* We must destroy this clone; first unmount it */ 407478f17100SMatthew Ahrens clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 407578f17100SMatthew Ahrens cbp->cb_force ? MS_FORCE: 0); 407678f17100SMatthew Ahrens if (clp == NULL || changelist_prefix(clp) != 0) { 407778f17100SMatthew Ahrens cbp->cb_error = B_TRUE; 407878f17100SMatthew Ahrens zfs_close(zhp); 407978f17100SMatthew Ahrens return (0); 408078f17100SMatthew Ahrens } 408178f17100SMatthew Ahrens if (zfs_destroy(zhp, B_FALSE) != 0) 408278f17100SMatthew Ahrens cbp->cb_error = B_TRUE; 408378f17100SMatthew Ahrens else 408478f17100SMatthew Ahrens changelist_remove(clp, zhp->zfs_name); 408578f17100SMatthew Ahrens (void) changelist_postfix(clp); 408678f17100SMatthew Ahrens changelist_free(clp); 4087b12a1c38Slling 408878f17100SMatthew Ahrens zfs_close(zhp); 408978f17100SMatthew Ahrens return (0); 409078f17100SMatthew Ahrens } 4091b12a1c38Slling 409278f17100SMatthew Ahrens static int 409378f17100SMatthew Ahrens rollback_destroy(zfs_handle_t *zhp, void *data) 409478f17100SMatthew Ahrens { 409578f17100SMatthew Ahrens rollback_data_t *cbp = data; 4096b12a1c38Slling 409778f17100SMatthew Ahrens if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) { 409878f17100SMatthew Ahrens cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE, 409978f17100SMatthew Ahrens rollback_destroy_dependent, cbp); 4100c391e322Sahrens 410178f17100SMatthew Ahrens cbp->cb_error |= zfs_destroy(zhp, B_FALSE); 4102b12a1c38Slling } 4103b12a1c38Slling 4104b12a1c38Slling zfs_close(zhp); 4105b12a1c38Slling return (0); 4106b12a1c38Slling } 4107b12a1c38Slling 4108b12a1c38Slling /* 4109b12a1c38Slling * Given a dataset, rollback to a specific snapshot, discarding any 4110b12a1c38Slling * data changes since then and making it the active dataset. 4111b12a1c38Slling * 411278f17100SMatthew Ahrens * Any snapshots and bookmarks more recent than the target are 411378f17100SMatthew Ahrens * destroyed, along with their dependents (i.e. clones). 4114b12a1c38Slling */ 4115b12a1c38Slling int 4116c391e322Sahrens zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force) 4117b12a1c38Slling { 4118b12a1c38Slling rollback_data_t cb = { 0 }; 41194ccbb6e7Sahrens int err; 41207b97dc1aSrm boolean_t restore_resv = 0; 4121f83b46baSPaul Dagnelie uint64_t old_volsize = 0, new_volsize; 41227b97dc1aSrm zfs_prop_t resv_prop; 4123b12a1c38Slling 41244ccbb6e7Sahrens assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM || 41254ccbb6e7Sahrens zhp->zfs_type == ZFS_TYPE_VOLUME); 4126b12a1c38Slling 4127b12a1c38Slling /* 41282e2c1355SMatthew Ahrens * Destroy all recent snapshots and their dependents. 4129b12a1c38Slling */ 4130c391e322Sahrens cb.cb_force = force; 4131b12a1c38Slling cb.cb_target = snap->zfs_name; 4132b12a1c38Slling cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG); 41330d8fa8f8SMartin Matuska (void) zfs_iter_snapshots(zhp, B_FALSE, rollback_destroy, &cb); 413478f17100SMatthew Ahrens (void) zfs_iter_bookmarks(zhp, rollback_destroy, &cb); 4135b12a1c38Slling 4136c391e322Sahrens if (cb.cb_error) 4137c391e322Sahrens return (-1); 4138b12a1c38Slling 4139b12a1c38Slling /* 4140b12a1c38Slling * Now that we have verified that the snapshot is the latest, 4141b12a1c38Slling * rollback to the given snapshot. 4142b12a1c38Slling */ 4143b12a1c38Slling 41447b97dc1aSrm if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 41457b97dc1aSrm if (zfs_which_resv_prop(zhp, &resv_prop) < 0) 41467b97dc1aSrm return (-1); 41477b97dc1aSrm old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 41487b97dc1aSrm restore_resv = 41497b97dc1aSrm (old_volsize == zfs_prop_get_int(zhp, resv_prop)); 41507b97dc1aSrm } 41514ccbb6e7Sahrens 4152b12a1c38Slling /* 415377b17137SAndriy Gapon * Pass both the filesystem and the wanted snapshot names, 415477b17137SAndriy Gapon * we would get an error back if the snapshot is destroyed or 415577b17137SAndriy Gapon * a new snapshot is created before this request is processed. 4156b12a1c38Slling */ 415777b17137SAndriy Gapon err = lzc_rollback_to(zhp->zfs_name, snap->zfs_name); 415895643f75SAndriy Gapon if (err != 0) { 415995643f75SAndriy Gapon char errbuf[1024]; 416095643f75SAndriy Gapon 416195643f75SAndriy Gapon (void) snprintf(errbuf, sizeof (errbuf), 41624ccbb6e7Sahrens dgettext(TEXT_DOMAIN, "cannot rollback '%s'"), 41634ccbb6e7Sahrens zhp->zfs_name); 416495643f75SAndriy Gapon switch (err) { 416595643f75SAndriy Gapon case EEXIST: 416695643f75SAndriy Gapon zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 416795643f75SAndriy Gapon "there is a snapshot or bookmark more recent " 416895643f75SAndriy Gapon "than '%s'"), snap->zfs_name); 416995643f75SAndriy Gapon (void) zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf); 417095643f75SAndriy Gapon break; 417195643f75SAndriy Gapon case ESRCH: 417295643f75SAndriy Gapon zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 417395643f75SAndriy Gapon "'%s' is not found among snapshots of '%s'"), 417495643f75SAndriy Gapon snap->zfs_name, zhp->zfs_name); 417595643f75SAndriy Gapon (void) zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf); 417695643f75SAndriy Gapon break; 417795643f75SAndriy Gapon case EINVAL: 417895643f75SAndriy Gapon (void) zfs_error(zhp->zfs_hdl, EZFS_BADTYPE, errbuf); 417995643f75SAndriy Gapon break; 418095643f75SAndriy Gapon default: 418195643f75SAndriy Gapon (void) zfs_standard_error(zhp->zfs_hdl, err, errbuf); 418295643f75SAndriy Gapon } 4183b9415e83Srm return (err); 4184b9415e83Srm } 41857b97dc1aSrm 41867b97dc1aSrm /* 41877b97dc1aSrm * For volumes, if the pre-rollback volsize matched the pre- 41887b97dc1aSrm * rollback reservation and the volsize has changed then set 41897b97dc1aSrm * the reservation property to the post-rollback volsize. 41907b97dc1aSrm * Make a new handle since the rollback closed the dataset. 41917b97dc1aSrm */ 4192b9415e83Srm if ((zhp->zfs_type == ZFS_TYPE_VOLUME) && 4193b9415e83Srm (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) { 41947b97dc1aSrm if (restore_resv) { 41957b97dc1aSrm new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 41967b97dc1aSrm if (old_volsize != new_volsize) 4197b9415e83Srm err = zfs_prop_set_int(zhp, resv_prop, 4198b9415e83Srm new_volsize); 41997b97dc1aSrm } 42007b97dc1aSrm zfs_close(zhp); 42014ccbb6e7Sahrens } 42024ccbb6e7Sahrens return (err); 4203b12a1c38Slling } 4204b12a1c38Slling 4205fa9e4066Sahrens /* 4206fa9e4066Sahrens * Renames the given dataset. 4207fa9e4066Sahrens */ 4208fa9e4066Sahrens int 42096a9cb0eaSEric Schrock zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive, 42106a9cb0eaSEric Schrock boolean_t force_unmount) 4211fa9e4066Sahrens { 421288f61deeSIgor Kozhukhov int ret = 0; 4213fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 4214fa9e4066Sahrens char *delim; 4215cdf5b4caSmmusante prop_changelist_t *cl = NULL; 4216cdf5b4caSmmusante zfs_handle_t *zhrp = NULL; 4217cdf5b4caSmmusante char *parentname = NULL; 42189adfa60dSMatthew Ahrens char parent[ZFS_MAX_DATASET_NAME_LEN]; 421999653d4eSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 422099653d4eSeschrock char errbuf[1024]; 4221fa9e4066Sahrens 4222fa9e4066Sahrens /* if we have the same exact name, just return success */ 4223fa9e4066Sahrens if (strcmp(zhp->zfs_name, target) == 0) 4224fa9e4066Sahrens return (0); 4225fa9e4066Sahrens 422699653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 422799653d4eSeschrock "cannot rename to '%s'"), target); 422899653d4eSeschrock 4229add927f8Sloli /* make sure source name is valid */ 4230add927f8Sloli if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE)) 4231add927f8Sloli return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 4232add927f8Sloli 4233fa9e4066Sahrens /* 4234fa9e4066Sahrens * Make sure the target name is valid 4235fa9e4066Sahrens */ 4236fa9e4066Sahrens if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 423798579b20Snd if ((strchr(target, '@') == NULL) || 423898579b20Snd *target == '@') { 423998579b20Snd /* 424098579b20Snd * Snapshot target name is abbreviated, 424198579b20Snd * reconstruct full dataset name 424298579b20Snd */ 424398579b20Snd (void) strlcpy(parent, zhp->zfs_name, 424498579b20Snd sizeof (parent)); 424598579b20Snd delim = strchr(parent, '@'); 424698579b20Snd if (strchr(target, '@') == NULL) 424798579b20Snd *(++delim) = '\0'; 424898579b20Snd else 424998579b20Snd *delim = '\0'; 425098579b20Snd (void) strlcat(parent, target, sizeof (parent)); 425198579b20Snd target = parent; 425298579b20Snd } else { 425398579b20Snd /* 425498579b20Snd * Make sure we're renaming within the same dataset. 425598579b20Snd */ 425698579b20Snd delim = strchr(target, '@'); 425798579b20Snd if (strncmp(zhp->zfs_name, target, delim - target) 425898579b20Snd != 0 || zhp->zfs_name[delim - target] != '@') { 425998579b20Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 426098579b20Snd "snapshots must be part of same " 426198579b20Snd "dataset")); 426298579b20Snd return (zfs_error(hdl, EZFS_CROSSTARGET, 4263b1b8ab34Slling errbuf)); 426498579b20Snd } 4265fa9e4066Sahrens } 42665ac95da7SSerapheim Dimitropoulos 4267f18faf3fSek if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 426898579b20Snd return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 4269fa9e4066Sahrens } else { 4270cdf5b4caSmmusante if (recursive) { 4271cdf5b4caSmmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4272cdf5b4caSmmusante "recursive rename must be a snapshot")); 4273cdf5b4caSmmusante return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 4274cdf5b4caSmmusante } 4275cdf5b4caSmmusante 4276f18faf3fSek if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 427798579b20Snd return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 4278e9dbad6fSeschrock 4279fa9e4066Sahrens /* validate parents */ 4280d41c4376SMark J Musante if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0) 4281fa9e4066Sahrens return (-1); 4282fa9e4066Sahrens 4283fa9e4066Sahrens /* make sure we're in the same pool */ 4284fa9e4066Sahrens verify((delim = strchr(target, '/')) != NULL); 4285fa9e4066Sahrens if (strncmp(zhp->zfs_name, target, delim - target) != 0 || 4286fa9e4066Sahrens zhp->zfs_name[delim - target] != '/') { 428799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 428899653d4eSeschrock "datasets must be within same pool")); 428999653d4eSeschrock return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 4290fa9e4066Sahrens } 4291f2fdf992Snd 4292f2fdf992Snd /* new name cannot be a child of the current dataset name */ 4293d41c4376SMark J Musante if (is_descendant(zhp->zfs_name, target)) { 4294f2fdf992Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4295d41c4376SMark J Musante "New dataset name cannot be a descendant of " 4296f2fdf992Snd "current dataset name")); 4297f2fdf992Snd return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 4298f2fdf992Snd } 4299fa9e4066Sahrens } 4300fa9e4066Sahrens 430199653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), 430299653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name); 430399653d4eSeschrock 4304fa9e4066Sahrens if (getzoneid() == GLOBAL_ZONEID && 4305fa9e4066Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 430699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 430799653d4eSeschrock "dataset is used in a non-global zone")); 430899653d4eSeschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 4309fa9e4066Sahrens } 4310fa9e4066Sahrens 4311cdf5b4caSmmusante if (recursive) { 4312f0c5ee21Smmusante parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name); 4313f0c5ee21Smmusante if (parentname == NULL) { 4314f0c5ee21Smmusante ret = -1; 4315f0c5ee21Smmusante goto error; 4316f0c5ee21Smmusante } 4317cdf5b4caSmmusante delim = strchr(parentname, '@'); 4318cdf5b4caSmmusante *delim = '\0'; 4319990b4856Slling zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET); 4320cdf5b4caSmmusante if (zhrp == NULL) { 4321f0c5ee21Smmusante ret = -1; 4322f0c5ee21Smmusante goto error; 4323cdf5b4caSmmusante } 432433cde0d0SMatthew Ahrens } else if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) { 43256a9cb0eaSEric Schrock if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0, 43266a9cb0eaSEric Schrock force_unmount ? MS_FORCE : 0)) == NULL) 4327cdf5b4caSmmusante return (-1); 4328cdf5b4caSmmusante 4329cdf5b4caSmmusante if (changelist_haszonedchild(cl)) { 4330cdf5b4caSmmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4331cdf5b4caSmmusante "child dataset with inherited mountpoint is used " 4332cdf5b4caSmmusante "in a non-global zone")); 4333cdf5b4caSmmusante (void) zfs_error(hdl, EZFS_ZONED, errbuf); 4334f83b46baSPaul Dagnelie ret = -1; 4335cdf5b4caSmmusante goto error; 4336cdf5b4caSmmusante } 4337cdf5b4caSmmusante 4338cdf5b4caSmmusante if ((ret = changelist_prefix(cl)) != 0) 4339cdf5b4caSmmusante goto error; 4340cdf5b4caSmmusante } 4341fa9e4066Sahrens 4342e9dbad6fSeschrock if (ZFS_IS_VOLUME(zhp)) 4343fa9e4066Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 4344fa9e4066Sahrens else 4345fa9e4066Sahrens zc.zc_objset_type = DMU_OST_ZFS; 4346fa9e4066Sahrens 434798579b20Snd (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 4348e9dbad6fSeschrock (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value)); 434998579b20Snd 4350cdf5b4caSmmusante zc.zc_cookie = recursive; 4351cdf5b4caSmmusante 4352ecd6cf80Smarks if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) { 4353cdf5b4caSmmusante /* 4354cdf5b4caSmmusante * if it was recursive, the one that actually failed will 4355cdf5b4caSmmusante * be in zc.zc_name 4356cdf5b4caSmmusante */ 4357cdf5b4caSmmusante (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 43583cb34c60Sahrens "cannot rename '%s'"), zc.zc_name); 4359cdf5b4caSmmusante 4360cdf5b4caSmmusante if (recursive && errno == EEXIST) { 4361cdf5b4caSmmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4362cdf5b4caSmmusante "a child dataset already has a snapshot " 4363cdf5b4caSmmusante "with the new name")); 4364a10acbd6Seschrock (void) zfs_error(hdl, EZFS_EXISTS, errbuf); 4365cdf5b4caSmmusante } else { 4366cdf5b4caSmmusante (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf); 4367cdf5b4caSmmusante } 4368fa9e4066Sahrens 4369fa9e4066Sahrens /* 4370fa9e4066Sahrens * On failure, we still want to remount any filesystems that 4371fa9e4066Sahrens * were previously mounted, so we don't alter the system state. 4372fa9e4066Sahrens */ 437333cde0d0SMatthew Ahrens if (cl != NULL) 4374cdf5b4caSmmusante (void) changelist_postfix(cl); 4375fa9e4066Sahrens } else { 437633cde0d0SMatthew Ahrens if (cl != NULL) { 4377cdf5b4caSmmusante changelist_rename(cl, zfs_get_name(zhp), target); 4378cdf5b4caSmmusante ret = changelist_postfix(cl); 4379cdf5b4caSmmusante } 4380fa9e4066Sahrens } 4381fa9e4066Sahrens 4382fa9e4066Sahrens error: 438333cde0d0SMatthew Ahrens if (parentname != NULL) { 4384cdf5b4caSmmusante free(parentname); 4385cdf5b4caSmmusante } 438633cde0d0SMatthew Ahrens if (zhrp != NULL) { 4387cdf5b4caSmmusante zfs_close(zhrp); 4388cdf5b4caSmmusante } 438933cde0d0SMatthew Ahrens if (cl != NULL) { 4390cdf5b4caSmmusante changelist_free(cl); 4391cdf5b4caSmmusante } 4392fa9e4066Sahrens return (ret); 4393fa9e4066Sahrens } 4394fa9e4066Sahrens 4395e9dbad6fSeschrock nvlist_t * 4396e9dbad6fSeschrock zfs_get_user_props(zfs_handle_t *zhp) 4397e9dbad6fSeschrock { 4398e9dbad6fSeschrock return (zhp->zfs_user_props); 4399e9dbad6fSeschrock } 4400e9dbad6fSeschrock 440192241e0bSTom Erickson nvlist_t * 440292241e0bSTom Erickson zfs_get_recvd_props(zfs_handle_t *zhp) 440392241e0bSTom Erickson { 440492241e0bSTom Erickson if (zhp->zfs_recvd_props == NULL) 440592241e0bSTom Erickson if (get_recvd_props_ioctl(zhp) != 0) 440692241e0bSTom Erickson return (NULL); 440792241e0bSTom Erickson return (zhp->zfs_recvd_props); 440892241e0bSTom Erickson } 440992241e0bSTom Erickson 4410b1b8ab34Slling /* 4411b1b8ab34Slling * This function is used by 'zfs list' to determine the exact set of columns to 4412b1b8ab34Slling * display, and their maximum widths. This does two main things: 4413b1b8ab34Slling * 4414b1b8ab34Slling * - If this is a list of all properties, then expand the list to include 4415b1b8ab34Slling * all native properties, and set a flag so that for each dataset we look 4416b1b8ab34Slling * for new unique user properties and add them to the list. 4417b1b8ab34Slling * 4418b1b8ab34Slling * - For non fixed-width properties, keep track of the maximum width seen 441992241e0bSTom Erickson * so that we can size the column appropriately. If the user has 442092241e0bSTom Erickson * requested received property values, we also need to compute the width 442192241e0bSTom Erickson * of the RECEIVED column. 4422b1b8ab34Slling */ 4423b1b8ab34Slling int 442443d68d68SYuri Pankov zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received, 442543d68d68SYuri Pankov boolean_t literal) 4426b1b8ab34Slling { 4427b1b8ab34Slling libzfs_handle_t *hdl = zhp->zfs_hdl; 4428990b4856Slling zprop_list_t *entry; 4429990b4856Slling zprop_list_t **last, **start; 4430b1b8ab34Slling nvlist_t *userprops, *propval; 4431b1b8ab34Slling nvpair_t *elem; 4432b1b8ab34Slling char *strval; 4433b1b8ab34Slling char buf[ZFS_MAXPROPLEN]; 4434b1b8ab34Slling 4435990b4856Slling if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0) 4436b1b8ab34Slling return (-1); 4437e9dbad6fSeschrock 4438e9dbad6fSeschrock userprops = zfs_get_user_props(zhp); 4439e9dbad6fSeschrock 4440e9dbad6fSeschrock entry = *plp; 4441e9dbad6fSeschrock if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) { 4442e9dbad6fSeschrock /* 4443e9dbad6fSeschrock * Go through and add any user properties as necessary. We 4444e9dbad6fSeschrock * start by incrementing our list pointer to the first 4445e9dbad6fSeschrock * non-native property. 4446e9dbad6fSeschrock */ 4447e9dbad6fSeschrock start = plp; 4448e9dbad6fSeschrock while (*start != NULL) { 4449990b4856Slling if ((*start)->pl_prop == ZPROP_INVAL) 4450e9dbad6fSeschrock break; 4451e9dbad6fSeschrock start = &(*start)->pl_next; 4452e9dbad6fSeschrock } 4453e9dbad6fSeschrock 4454e9dbad6fSeschrock elem = NULL; 4455e9dbad6fSeschrock while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) { 4456e9dbad6fSeschrock /* 4457e9dbad6fSeschrock * See if we've already found this property in our list. 4458e9dbad6fSeschrock */ 4459e9dbad6fSeschrock for (last = start; *last != NULL; 4460e9dbad6fSeschrock last = &(*last)->pl_next) { 4461e9dbad6fSeschrock if (strcmp((*last)->pl_user_prop, 4462e9dbad6fSeschrock nvpair_name(elem)) == 0) 4463e9dbad6fSeschrock break; 4464e9dbad6fSeschrock } 4465e9dbad6fSeschrock 4466e9dbad6fSeschrock if (*last == NULL) { 4467e9dbad6fSeschrock if ((entry = zfs_alloc(hdl, 4468990b4856Slling sizeof (zprop_list_t))) == NULL || 4469e9dbad6fSeschrock ((entry->pl_user_prop = zfs_strdup(hdl, 4470e9dbad6fSeschrock nvpair_name(elem)))) == NULL) { 4471e9dbad6fSeschrock free(entry); 4472e9dbad6fSeschrock return (-1); 4473e9dbad6fSeschrock } 4474e9dbad6fSeschrock 4475990b4856Slling entry->pl_prop = ZPROP_INVAL; 4476e9dbad6fSeschrock entry->pl_width = strlen(nvpair_name(elem)); 4477e9dbad6fSeschrock entry->pl_all = B_TRUE; 4478e9dbad6fSeschrock *last = entry; 4479e9dbad6fSeschrock } 4480e9dbad6fSeschrock } 4481e9dbad6fSeschrock } 4482e9dbad6fSeschrock 4483e9dbad6fSeschrock /* 4484e9dbad6fSeschrock * Now go through and check the width of any non-fixed columns 4485e9dbad6fSeschrock */ 4486e9dbad6fSeschrock for (entry = *plp; entry != NULL; entry = entry->pl_next) { 448743d68d68SYuri Pankov if (entry->pl_fixed && !literal) 4488e9dbad6fSeschrock continue; 4489e9dbad6fSeschrock 4490990b4856Slling if (entry->pl_prop != ZPROP_INVAL) { 4491e9dbad6fSeschrock if (zfs_prop_get(zhp, entry->pl_prop, 449243d68d68SYuri Pankov buf, sizeof (buf), NULL, NULL, 0, literal) == 0) { 4493e9dbad6fSeschrock if (strlen(buf) > entry->pl_width) 4494e9dbad6fSeschrock entry->pl_width = strlen(buf); 4495e9dbad6fSeschrock } 449692241e0bSTom Erickson if (received && zfs_prop_get_recvd(zhp, 449792241e0bSTom Erickson zfs_prop_to_name(entry->pl_prop), 449843d68d68SYuri Pankov buf, sizeof (buf), literal) == 0) 449992241e0bSTom Erickson if (strlen(buf) > entry->pl_recvd_width) 450092241e0bSTom Erickson entry->pl_recvd_width = strlen(buf); 450192241e0bSTom Erickson } else { 450292241e0bSTom Erickson if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop, 450392241e0bSTom Erickson &propval) == 0) { 450492241e0bSTom Erickson verify(nvlist_lookup_string(propval, 450592241e0bSTom Erickson ZPROP_VALUE, &strval) == 0); 450692241e0bSTom Erickson if (strlen(strval) > entry->pl_width) 450792241e0bSTom Erickson entry->pl_width = strlen(strval); 450892241e0bSTom Erickson } 450992241e0bSTom Erickson if (received && zfs_prop_get_recvd(zhp, 451092241e0bSTom Erickson entry->pl_user_prop, 451143d68d68SYuri Pankov buf, sizeof (buf), literal) == 0) 451292241e0bSTom Erickson if (strlen(buf) > entry->pl_recvd_width) 451392241e0bSTom Erickson entry->pl_recvd_width = strlen(buf); 4514e9dbad6fSeschrock } 4515e9dbad6fSeschrock } 4516e9dbad6fSeschrock 4517e9dbad6fSeschrock return (0); 4518e9dbad6fSeschrock } 4519ecd6cf80Smarks 4520ecd6cf80Smarks int 4521ecd6cf80Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path, 4522743a77edSAlan Wright char *resource, void *export, void *sharetab, 4523743a77edSAlan Wright int sharemax, zfs_share_op_t operation) 4524ecd6cf80Smarks { 4525ecd6cf80Smarks zfs_cmd_t zc = { 0 }; 4526ecd6cf80Smarks int error; 4527ecd6cf80Smarks 4528ecd6cf80Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 4529ecd6cf80Smarks (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value)); 4530743a77edSAlan Wright if (resource) 4531743a77edSAlan Wright (void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string)); 4532ecd6cf80Smarks zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab; 4533ecd6cf80Smarks zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export; 4534da6c28aaSamw zc.zc_share.z_sharetype = operation; 4535ecd6cf80Smarks zc.zc_share.z_sharemax = sharemax; 4536ecd6cf80Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc); 4537ecd6cf80Smarks return (error); 4538ecd6cf80Smarks } 45392e5e9e19SSanjeev Bagewadi 45402e5e9e19SSanjeev Bagewadi void 45412e5e9e19SSanjeev Bagewadi zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props) 45422e5e9e19SSanjeev Bagewadi { 45432e5e9e19SSanjeev Bagewadi nvpair_t *curr; 45442e5e9e19SSanjeev Bagewadi 45452e5e9e19SSanjeev Bagewadi /* 45462e5e9e19SSanjeev Bagewadi * Keep a reference to the props-table against which we prune the 45472e5e9e19SSanjeev Bagewadi * properties. 45482e5e9e19SSanjeev Bagewadi */ 45492e5e9e19SSanjeev Bagewadi zhp->zfs_props_table = props; 45502e5e9e19SSanjeev Bagewadi 45512e5e9e19SSanjeev Bagewadi curr = nvlist_next_nvpair(zhp->zfs_props, NULL); 45522e5e9e19SSanjeev Bagewadi 45532e5e9e19SSanjeev Bagewadi while (curr) { 45542e5e9e19SSanjeev Bagewadi zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr)); 45552e5e9e19SSanjeev Bagewadi nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr); 45562e5e9e19SSanjeev Bagewadi 455714843421SMatthew Ahrens /* 4558faaa6415SEric Schrock * User properties will result in ZPROP_INVAL, and since we 4559faaa6415SEric Schrock * only know how to prune standard ZFS properties, we always 4560faaa6415SEric Schrock * leave these in the list. This can also happen if we 4561faaa6415SEric Schrock * encounter an unknown DSL property (when running older 4562faaa6415SEric Schrock * software, for example). 456314843421SMatthew Ahrens */ 456414843421SMatthew Ahrens if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE) 45652e5e9e19SSanjeev Bagewadi (void) nvlist_remove(zhp->zfs_props, 45662e5e9e19SSanjeev Bagewadi nvpair_name(curr), nvpair_type(curr)); 45672e5e9e19SSanjeev Bagewadi curr = next; 45682e5e9e19SSanjeev Bagewadi } 45692e5e9e19SSanjeev Bagewadi } 4570743a77edSAlan Wright 4571743a77edSAlan Wright static int 4572743a77edSAlan Wright zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path, 4573743a77edSAlan Wright zfs_smb_acl_op_t cmd, char *resource1, char *resource2) 4574743a77edSAlan Wright { 4575743a77edSAlan Wright zfs_cmd_t zc = { 0 }; 4576743a77edSAlan Wright nvlist_t *nvlist = NULL; 4577743a77edSAlan Wright int error; 4578743a77edSAlan Wright 4579743a77edSAlan Wright (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 4580743a77edSAlan Wright (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value)); 4581743a77edSAlan Wright zc.zc_cookie = (uint64_t)cmd; 4582743a77edSAlan Wright 4583743a77edSAlan Wright if (cmd == ZFS_SMB_ACL_RENAME) { 4584743a77edSAlan Wright if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) { 4585743a77edSAlan Wright (void) no_memory(hdl); 458630925561SChris Williamson return (0); 4587743a77edSAlan Wright } 4588743a77edSAlan Wright } 4589743a77edSAlan Wright 4590743a77edSAlan Wright switch (cmd) { 4591743a77edSAlan Wright case ZFS_SMB_ACL_ADD: 4592743a77edSAlan Wright case ZFS_SMB_ACL_REMOVE: 4593743a77edSAlan Wright (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string)); 4594743a77edSAlan Wright break; 4595743a77edSAlan Wright case ZFS_SMB_ACL_RENAME: 4596743a77edSAlan Wright if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC, 4597743a77edSAlan Wright resource1) != 0) { 4598743a77edSAlan Wright (void) no_memory(hdl); 4599743a77edSAlan Wright return (-1); 4600743a77edSAlan Wright } 4601743a77edSAlan Wright if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET, 4602743a77edSAlan Wright resource2) != 0) { 4603743a77edSAlan Wright (void) no_memory(hdl); 4604743a77edSAlan Wright return (-1); 4605743a77edSAlan Wright } 4606743a77edSAlan Wright if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) { 4607743a77edSAlan Wright nvlist_free(nvlist); 4608743a77edSAlan Wright return (-1); 4609743a77edSAlan Wright } 4610743a77edSAlan Wright break; 4611743a77edSAlan Wright case ZFS_SMB_ACL_PURGE: 4612743a77edSAlan Wright break; 4613743a77edSAlan Wright default: 4614743a77edSAlan Wright return (-1); 4615743a77edSAlan Wright } 4616743a77edSAlan Wright error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc); 4617aab83bb8SJosef 'Jeff' Sipek nvlist_free(nvlist); 4618743a77edSAlan Wright return (error); 4619743a77edSAlan Wright } 4620743a77edSAlan Wright 4621743a77edSAlan Wright int 4622743a77edSAlan Wright zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset, 4623743a77edSAlan Wright char *path, char *resource) 4624743a77edSAlan Wright { 4625743a77edSAlan Wright return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD, 4626743a77edSAlan Wright resource, NULL)); 4627743a77edSAlan Wright } 4628743a77edSAlan Wright 4629743a77edSAlan Wright int 4630743a77edSAlan Wright zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset, 4631743a77edSAlan Wright char *path, char *resource) 4632743a77edSAlan Wright { 4633743a77edSAlan Wright return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE, 4634743a77edSAlan Wright resource, NULL)); 4635743a77edSAlan Wright } 4636743a77edSAlan Wright 4637743a77edSAlan Wright int 4638743a77edSAlan Wright zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path) 4639743a77edSAlan Wright { 4640743a77edSAlan Wright return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE, 4641743a77edSAlan Wright NULL, NULL)); 4642743a77edSAlan Wright } 4643743a77edSAlan Wright 4644743a77edSAlan Wright int 4645743a77edSAlan Wright zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path, 4646743a77edSAlan Wright char *oldname, char *newname) 4647743a77edSAlan Wright { 4648743a77edSAlan Wright return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME, 4649743a77edSAlan Wright oldname, newname)); 4650743a77edSAlan Wright } 465114843421SMatthew Ahrens 465214843421SMatthew Ahrens int 465314843421SMatthew Ahrens zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type, 465414843421SMatthew Ahrens zfs_userspace_cb_t func, void *arg) 465514843421SMatthew Ahrens { 465614843421SMatthew Ahrens zfs_cmd_t zc = { 0 }; 465714843421SMatthew Ahrens zfs_useracct_t buf[100]; 465870f56fa6SYuri Pankov libzfs_handle_t *hdl = zhp->zfs_hdl; 465970f56fa6SYuri Pankov int ret; 466014843421SMatthew Ahrens 466119b94df9SMatthew Ahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 466214843421SMatthew Ahrens 466314843421SMatthew Ahrens zc.zc_objset_type = type; 466414843421SMatthew Ahrens zc.zc_nvlist_dst = (uintptr_t)buf; 466514843421SMatthew Ahrens 466670f56fa6SYuri Pankov for (;;) { 466714843421SMatthew Ahrens zfs_useracct_t *zua = buf; 466814843421SMatthew Ahrens 466914843421SMatthew Ahrens zc.zc_nvlist_dst_size = sizeof (buf); 467070f56fa6SYuri Pankov if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) { 46713b2aab18SMatthew Ahrens char errbuf[1024]; 467270f56fa6SYuri Pankov 467370f56fa6SYuri Pankov (void) snprintf(errbuf, sizeof (errbuf), 467470f56fa6SYuri Pankov dgettext(TEXT_DOMAIN, 467570f56fa6SYuri Pankov "cannot get used/quota for %s"), zc.zc_name); 467670f56fa6SYuri Pankov return (zfs_standard_error_fmt(hdl, errno, errbuf)); 467770f56fa6SYuri Pankov } 467870f56fa6SYuri Pankov if (zc.zc_nvlist_dst_size == 0) 467914843421SMatthew Ahrens break; 468014843421SMatthew Ahrens 468114843421SMatthew Ahrens while (zc.zc_nvlist_dst_size > 0) { 468270f56fa6SYuri Pankov if ((ret = func(arg, zua->zu_domain, zua->zu_rid, 468370f56fa6SYuri Pankov zua->zu_space)) != 0) 468470f56fa6SYuri Pankov return (ret); 468514843421SMatthew Ahrens zua++; 468614843421SMatthew Ahrens zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t); 468714843421SMatthew Ahrens } 468814843421SMatthew Ahrens } 468914843421SMatthew Ahrens 469070f56fa6SYuri Pankov return (0); 469114843421SMatthew Ahrens } 4692842727c2SChris Kirby 46933b2aab18SMatthew Ahrens struct holdarg { 46943b2aab18SMatthew Ahrens nvlist_t *nvl; 46953b2aab18SMatthew Ahrens const char *snapname; 46963b2aab18SMatthew Ahrens const char *tag; 46973b2aab18SMatthew Ahrens boolean_t recursive; 4698bb6e7075SMatthew Ahrens int error; 46993b2aab18SMatthew Ahrens }; 47003b2aab18SMatthew Ahrens 47013b2aab18SMatthew Ahrens static int 47023b2aab18SMatthew Ahrens zfs_hold_one(zfs_handle_t *zhp, void *arg) 47033b2aab18SMatthew Ahrens { 47043b2aab18SMatthew Ahrens struct holdarg *ha = arg; 47059adfa60dSMatthew Ahrens char name[ZFS_MAX_DATASET_NAME_LEN]; 47063b2aab18SMatthew Ahrens int rv = 0; 47073b2aab18SMatthew Ahrens 47083b2aab18SMatthew Ahrens (void) snprintf(name, sizeof (name), 47093b2aab18SMatthew Ahrens "%s@%s", zhp->zfs_name, ha->snapname); 47103b2aab18SMatthew Ahrens 4711a7a845e4SSteven Hartland if (lzc_exists(name)) 47123b2aab18SMatthew Ahrens fnvlist_add_string(ha->nvl, name, ha->tag); 47133b2aab18SMatthew Ahrens 47143b2aab18SMatthew Ahrens if (ha->recursive) 47153b2aab18SMatthew Ahrens rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha); 47163b2aab18SMatthew Ahrens zfs_close(zhp); 47173b2aab18SMatthew Ahrens return (rv); 47183b2aab18SMatthew Ahrens } 47193b2aab18SMatthew Ahrens 4720842727c2SChris Kirby int 4721842727c2SChris Kirby zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag, 4722a7a845e4SSteven Hartland boolean_t recursive, int cleanup_fd) 4723842727c2SChris Kirby { 47243b2aab18SMatthew Ahrens int ret; 47253b2aab18SMatthew Ahrens struct holdarg ha; 4726842727c2SChris Kirby 47273b2aab18SMatthew Ahrens ha.nvl = fnvlist_alloc(); 47283b2aab18SMatthew Ahrens ha.snapname = snapname; 47293b2aab18SMatthew Ahrens ha.tag = tag; 47303b2aab18SMatthew Ahrens ha.recursive = recursive; 47313b2aab18SMatthew Ahrens (void) zfs_hold_one(zfs_handle_dup(zhp), &ha); 4732013023d4SMartin Matuska 4733a7a845e4SSteven Hartland if (nvlist_empty(ha.nvl)) { 4734a7a845e4SSteven Hartland char errbuf[1024]; 4735a7a845e4SSteven Hartland 4736013023d4SMartin Matuska fnvlist_free(ha.nvl); 4737013023d4SMartin Matuska ret = ENOENT; 4738a7a845e4SSteven Hartland (void) snprintf(errbuf, sizeof (errbuf), 4739a7a845e4SSteven Hartland dgettext(TEXT_DOMAIN, 4740a7a845e4SSteven Hartland "cannot hold snapshot '%s@%s'"), 4741a7a845e4SSteven Hartland zhp->zfs_name, snapname); 4742a7a845e4SSteven Hartland (void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf); 4743013023d4SMartin Matuska return (ret); 4744013023d4SMartin Matuska } 4745013023d4SMartin Matuska 4746a7a845e4SSteven Hartland ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl); 47473b2aab18SMatthew Ahrens fnvlist_free(ha.nvl); 4748a7f53a56SChris Kirby 4749a7a845e4SSteven Hartland return (ret); 4750a7a845e4SSteven Hartland } 4751a7a845e4SSteven Hartland 4752a7a845e4SSteven Hartland int 4753a7a845e4SSteven Hartland zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds) 4754a7a845e4SSteven Hartland { 4755a7a845e4SSteven Hartland int ret; 4756a7a845e4SSteven Hartland nvlist_t *errors; 4757a7a845e4SSteven Hartland libzfs_handle_t *hdl = zhp->zfs_hdl; 4758a7a845e4SSteven Hartland char errbuf[1024]; 4759a7a845e4SSteven Hartland nvpair_t *elem; 4760a7a845e4SSteven Hartland 4761a7a845e4SSteven Hartland errors = NULL; 4762a7a845e4SSteven Hartland ret = lzc_hold(holds, cleanup_fd, &errors); 4763a7a845e4SSteven Hartland 4764a7a845e4SSteven Hartland if (ret == 0) { 4765a7a845e4SSteven Hartland /* There may be errors even in the success case. */ 4766a7a845e4SSteven Hartland fnvlist_free(errors); 47673b2aab18SMatthew Ahrens return (0); 4768a7a845e4SSteven Hartland } 4769842727c2SChris Kirby 4770a7a845e4SSteven Hartland if (nvlist_empty(errors)) { 47713b2aab18SMatthew Ahrens /* no hold-specific errors */ 47723b2aab18SMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), 47733b2aab18SMatthew Ahrens dgettext(TEXT_DOMAIN, "cannot hold")); 47743b2aab18SMatthew Ahrens switch (ret) { 47753b2aab18SMatthew Ahrens case ENOTSUP: 47763b2aab18SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 47773b2aab18SMatthew Ahrens "pool must be upgraded")); 47783b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 47793b2aab18SMatthew Ahrens break; 47803b2aab18SMatthew Ahrens case EINVAL: 47813b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 47823b2aab18SMatthew Ahrens break; 47833b2aab18SMatthew Ahrens default: 47843b2aab18SMatthew Ahrens (void) zfs_standard_error(hdl, ret, errbuf); 47853b2aab18SMatthew Ahrens } 47863b2aab18SMatthew Ahrens } 4787842727c2SChris Kirby 47883b2aab18SMatthew Ahrens for (elem = nvlist_next_nvpair(errors, NULL); 47893b2aab18SMatthew Ahrens elem != NULL; 47903b2aab18SMatthew Ahrens elem = nvlist_next_nvpair(errors, elem)) { 47913b2aab18SMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), 47923b2aab18SMatthew Ahrens dgettext(TEXT_DOMAIN, 47933b2aab18SMatthew Ahrens "cannot hold snapshot '%s'"), nvpair_name(elem)); 47943b2aab18SMatthew Ahrens switch (fnvpair_value_int32(elem)) { 479515508ac0SChris Kirby case E2BIG: 479615508ac0SChris Kirby /* 479715508ac0SChris Kirby * Temporary tags wind up having the ds object id 479815508ac0SChris Kirby * prepended. So even if we passed the length check 479915508ac0SChris Kirby * above, it's still possible for the tag to wind 480015508ac0SChris Kirby * up being slightly too long. 480115508ac0SChris Kirby */ 48023b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf); 48033b2aab18SMatthew Ahrens break; 4804842727c2SChris Kirby case EINVAL: 48053b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 48063b2aab18SMatthew Ahrens break; 4807842727c2SChris Kirby case EEXIST: 48083b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf); 48093b2aab18SMatthew Ahrens break; 4810842727c2SChris Kirby default: 48113b2aab18SMatthew Ahrens (void) zfs_standard_error(hdl, 48123b2aab18SMatthew Ahrens fnvpair_value_int32(elem), errbuf); 4813842727c2SChris Kirby } 4814842727c2SChris Kirby } 4815842727c2SChris Kirby 48163b2aab18SMatthew Ahrens fnvlist_free(errors); 48173b2aab18SMatthew Ahrens return (ret); 48183b2aab18SMatthew Ahrens } 48193b2aab18SMatthew Ahrens 48203b2aab18SMatthew Ahrens static int 48213b2aab18SMatthew Ahrens zfs_release_one(zfs_handle_t *zhp, void *arg) 48223b2aab18SMatthew Ahrens { 48233b2aab18SMatthew Ahrens struct holdarg *ha = arg; 48249adfa60dSMatthew Ahrens char name[ZFS_MAX_DATASET_NAME_LEN]; 48253b2aab18SMatthew Ahrens int rv = 0; 4826bb6e7075SMatthew Ahrens nvlist_t *existing_holds; 48273b2aab18SMatthew Ahrens 48283b2aab18SMatthew Ahrens (void) snprintf(name, sizeof (name), 48293b2aab18SMatthew Ahrens "%s@%s", zhp->zfs_name, ha->snapname); 48303b2aab18SMatthew Ahrens 4831bb6e7075SMatthew Ahrens if (lzc_get_holds(name, &existing_holds) != 0) { 4832bb6e7075SMatthew Ahrens ha->error = ENOENT; 4833bb6e7075SMatthew Ahrens } else if (!nvlist_exists(existing_holds, ha->tag)) { 4834bb6e7075SMatthew Ahrens ha->error = ESRCH; 4835bb6e7075SMatthew Ahrens } else { 4836bb6e7075SMatthew Ahrens nvlist_t *torelease = fnvlist_alloc(); 4837bb6e7075SMatthew Ahrens fnvlist_add_boolean(torelease, ha->tag); 4838bb6e7075SMatthew Ahrens fnvlist_add_nvlist(ha->nvl, name, torelease); 4839bb6e7075SMatthew Ahrens fnvlist_free(torelease); 48403b2aab18SMatthew Ahrens } 48413b2aab18SMatthew Ahrens 48423b2aab18SMatthew Ahrens if (ha->recursive) 48433b2aab18SMatthew Ahrens rv = zfs_iter_filesystems(zhp, zfs_release_one, ha); 48443b2aab18SMatthew Ahrens zfs_close(zhp); 48453b2aab18SMatthew Ahrens return (rv); 4846842727c2SChris Kirby } 4847842727c2SChris Kirby 4848842727c2SChris Kirby int 4849842727c2SChris Kirby zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag, 4850842727c2SChris Kirby boolean_t recursive) 4851842727c2SChris Kirby { 48523b2aab18SMatthew Ahrens int ret; 48533b2aab18SMatthew Ahrens struct holdarg ha; 4854a7a845e4SSteven Hartland nvlist_t *errors = NULL; 48553b2aab18SMatthew Ahrens nvpair_t *elem; 4856842727c2SChris Kirby libzfs_handle_t *hdl = zhp->zfs_hdl; 4857013023d4SMartin Matuska char errbuf[1024]; 4858842727c2SChris Kirby 48593b2aab18SMatthew Ahrens ha.nvl = fnvlist_alloc(); 48603b2aab18SMatthew Ahrens ha.snapname = snapname; 48613b2aab18SMatthew Ahrens ha.tag = tag; 48623b2aab18SMatthew Ahrens ha.recursive = recursive; 4863bb6e7075SMatthew Ahrens ha.error = 0; 48643b2aab18SMatthew Ahrens (void) zfs_release_one(zfs_handle_dup(zhp), &ha); 4865013023d4SMartin Matuska 4866a7a845e4SSteven Hartland if (nvlist_empty(ha.nvl)) { 4867013023d4SMartin Matuska fnvlist_free(ha.nvl); 4868bb6e7075SMatthew Ahrens ret = ha.error; 4869013023d4SMartin Matuska (void) snprintf(errbuf, sizeof (errbuf), 4870013023d4SMartin Matuska dgettext(TEXT_DOMAIN, 4871013023d4SMartin Matuska "cannot release hold from snapshot '%s@%s'"), 4872013023d4SMartin Matuska zhp->zfs_name, snapname); 4873bb6e7075SMatthew Ahrens if (ret == ESRCH) { 4874bb6e7075SMatthew Ahrens (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf); 4875bb6e7075SMatthew Ahrens } else { 4876bb6e7075SMatthew Ahrens (void) zfs_standard_error(hdl, ret, errbuf); 4877bb6e7075SMatthew Ahrens } 4878013023d4SMartin Matuska return (ret); 4879013023d4SMartin Matuska } 4880013023d4SMartin Matuska 48813b2aab18SMatthew Ahrens ret = lzc_release(ha.nvl, &errors); 48823b2aab18SMatthew Ahrens fnvlist_free(ha.nvl); 4883842727c2SChris Kirby 4884a7a845e4SSteven Hartland if (ret == 0) { 4885a7a845e4SSteven Hartland /* There may be errors even in the success case. */ 4886a7a845e4SSteven Hartland fnvlist_free(errors); 48873b2aab18SMatthew Ahrens return (0); 4888a7a845e4SSteven Hartland } 48893b2aab18SMatthew Ahrens 4890a7a845e4SSteven Hartland if (nvlist_empty(errors)) { 48913b2aab18SMatthew Ahrens /* no hold-specific errors */ 4892842727c2SChris Kirby (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 48933b2aab18SMatthew Ahrens "cannot release")); 4894842727c2SChris Kirby switch (errno) { 4895842727c2SChris Kirby case ENOTSUP: 4896842727c2SChris Kirby zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4897842727c2SChris Kirby "pool must be upgraded")); 48983b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 48993b2aab18SMatthew Ahrens break; 49003b2aab18SMatthew Ahrens default: 49013b2aab18SMatthew Ahrens (void) zfs_standard_error_fmt(hdl, errno, errbuf); 49023b2aab18SMatthew Ahrens } 49033b2aab18SMatthew Ahrens } 49043b2aab18SMatthew Ahrens 49053b2aab18SMatthew Ahrens for (elem = nvlist_next_nvpair(errors, NULL); 49063b2aab18SMatthew Ahrens elem != NULL; 49073b2aab18SMatthew Ahrens elem = nvlist_next_nvpair(errors, elem)) { 49083b2aab18SMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), 49093b2aab18SMatthew Ahrens dgettext(TEXT_DOMAIN, 49103b2aab18SMatthew Ahrens "cannot release hold from snapshot '%s'"), 49113b2aab18SMatthew Ahrens nvpair_name(elem)); 49123b2aab18SMatthew Ahrens switch (fnvpair_value_int32(elem)) { 49133b2aab18SMatthew Ahrens case ESRCH: 49143b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf); 49153b2aab18SMatthew Ahrens break; 4916842727c2SChris Kirby case EINVAL: 49173b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 49183b2aab18SMatthew Ahrens break; 4919842727c2SChris Kirby default: 49203b2aab18SMatthew Ahrens (void) zfs_standard_error_fmt(hdl, 49213b2aab18SMatthew Ahrens fnvpair_value_int32(elem), errbuf); 4922842727c2SChris Kirby } 4923842727c2SChris Kirby } 4924842727c2SChris Kirby 49253b2aab18SMatthew Ahrens fnvlist_free(errors); 49263b2aab18SMatthew Ahrens return (ret); 4927842727c2SChris Kirby } 4928ca45db41SChris Kirby 49291af68beaSAlexander Stetsenko int 49301af68beaSAlexander Stetsenko zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl) 49311af68beaSAlexander Stetsenko { 49321af68beaSAlexander Stetsenko zfs_cmd_t zc = { 0 }; 49331af68beaSAlexander Stetsenko libzfs_handle_t *hdl = zhp->zfs_hdl; 49341af68beaSAlexander Stetsenko int nvsz = 2048; 49351af68beaSAlexander Stetsenko void *nvbuf; 49361af68beaSAlexander Stetsenko int err = 0; 49373b2aab18SMatthew Ahrens char errbuf[1024]; 49381af68beaSAlexander Stetsenko 49391af68beaSAlexander Stetsenko assert(zhp->zfs_type == ZFS_TYPE_VOLUME || 49401af68beaSAlexander Stetsenko zhp->zfs_type == ZFS_TYPE_FILESYSTEM); 49411af68beaSAlexander Stetsenko 49421af68beaSAlexander Stetsenko tryagain: 49431af68beaSAlexander Stetsenko 49441af68beaSAlexander Stetsenko nvbuf = malloc(nvsz); 49451af68beaSAlexander Stetsenko if (nvbuf == NULL) { 49461af68beaSAlexander Stetsenko err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno))); 49471af68beaSAlexander Stetsenko goto out; 49481af68beaSAlexander Stetsenko } 49491af68beaSAlexander Stetsenko 49501af68beaSAlexander Stetsenko zc.zc_nvlist_dst_size = nvsz; 49511af68beaSAlexander Stetsenko zc.zc_nvlist_dst = (uintptr_t)nvbuf; 49521af68beaSAlexander Stetsenko 49539adfa60dSMatthew Ahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 49541af68beaSAlexander Stetsenko 4955d7f601efSGeorge Wilson if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) { 49561af68beaSAlexander Stetsenko (void) snprintf(errbuf, sizeof (errbuf), 49571af68beaSAlexander Stetsenko dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"), 49581af68beaSAlexander Stetsenko zc.zc_name); 49591af68beaSAlexander Stetsenko switch (errno) { 49601af68beaSAlexander Stetsenko case ENOMEM: 49611af68beaSAlexander Stetsenko free(nvbuf); 49621af68beaSAlexander Stetsenko nvsz = zc.zc_nvlist_dst_size; 49631af68beaSAlexander Stetsenko goto tryagain; 49641af68beaSAlexander Stetsenko 49651af68beaSAlexander Stetsenko case ENOTSUP: 49661af68beaSAlexander Stetsenko zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 49671af68beaSAlexander Stetsenko "pool must be upgraded")); 49681af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADVERSION, errbuf); 49691af68beaSAlexander Stetsenko break; 49701af68beaSAlexander Stetsenko case EINVAL: 49711af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADTYPE, errbuf); 49721af68beaSAlexander Stetsenko break; 49731af68beaSAlexander Stetsenko case ENOENT: 49741af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_NOENT, errbuf); 49751af68beaSAlexander Stetsenko break; 49761af68beaSAlexander Stetsenko default: 49771af68beaSAlexander Stetsenko err = zfs_standard_error_fmt(hdl, errno, errbuf); 49781af68beaSAlexander Stetsenko break; 49791af68beaSAlexander Stetsenko } 49801af68beaSAlexander Stetsenko } else { 49811af68beaSAlexander Stetsenko /* success */ 49821af68beaSAlexander Stetsenko int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0); 49831af68beaSAlexander Stetsenko if (rc) { 49841af68beaSAlexander Stetsenko (void) snprintf(errbuf, sizeof (errbuf), dgettext( 49851af68beaSAlexander Stetsenko TEXT_DOMAIN, "cannot get permissions on '%s'"), 49861af68beaSAlexander Stetsenko zc.zc_name); 49871af68beaSAlexander Stetsenko err = zfs_standard_error_fmt(hdl, rc, errbuf); 49881af68beaSAlexander Stetsenko } 49891af68beaSAlexander Stetsenko } 49901af68beaSAlexander Stetsenko 49911af68beaSAlexander Stetsenko free(nvbuf); 49921af68beaSAlexander Stetsenko out: 49931af68beaSAlexander Stetsenko return (err); 49941af68beaSAlexander Stetsenko } 49951af68beaSAlexander Stetsenko 49961af68beaSAlexander Stetsenko int 49971af68beaSAlexander Stetsenko zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl) 49981af68beaSAlexander Stetsenko { 49991af68beaSAlexander Stetsenko zfs_cmd_t zc = { 0 }; 50001af68beaSAlexander Stetsenko libzfs_handle_t *hdl = zhp->zfs_hdl; 50011af68beaSAlexander Stetsenko char *nvbuf; 50023b2aab18SMatthew Ahrens char errbuf[1024]; 50031af68beaSAlexander Stetsenko size_t nvsz; 50041af68beaSAlexander Stetsenko int err; 50051af68beaSAlexander Stetsenko 50061af68beaSAlexander Stetsenko assert(zhp->zfs_type == ZFS_TYPE_VOLUME || 50071af68beaSAlexander Stetsenko zhp->zfs_type == ZFS_TYPE_FILESYSTEM); 50081af68beaSAlexander Stetsenko 50091af68beaSAlexander Stetsenko err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE); 50101af68beaSAlexander Stetsenko assert(err == 0); 50111af68beaSAlexander Stetsenko 50121af68beaSAlexander Stetsenko nvbuf = malloc(nvsz); 50131af68beaSAlexander Stetsenko 50141af68beaSAlexander Stetsenko err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0); 50151af68beaSAlexander Stetsenko assert(err == 0); 50161af68beaSAlexander Stetsenko 50171af68beaSAlexander Stetsenko zc.zc_nvlist_src_size = nvsz; 50181af68beaSAlexander Stetsenko zc.zc_nvlist_src = (uintptr_t)nvbuf; 50191af68beaSAlexander Stetsenko zc.zc_perm_action = un; 50201af68beaSAlexander Stetsenko 50211af68beaSAlexander Stetsenko (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 50221af68beaSAlexander Stetsenko 50231af68beaSAlexander Stetsenko if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) { 50241af68beaSAlexander Stetsenko (void) snprintf(errbuf, sizeof (errbuf), 50251af68beaSAlexander Stetsenko dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"), 50261af68beaSAlexander Stetsenko zc.zc_name); 50271af68beaSAlexander Stetsenko switch (errno) { 50281af68beaSAlexander Stetsenko case ENOTSUP: 50291af68beaSAlexander Stetsenko zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 50301af68beaSAlexander Stetsenko "pool must be upgraded")); 50311af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADVERSION, errbuf); 50321af68beaSAlexander Stetsenko break; 50331af68beaSAlexander Stetsenko case EINVAL: 50341af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADTYPE, errbuf); 50351af68beaSAlexander Stetsenko break; 50361af68beaSAlexander Stetsenko case ENOENT: 50371af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_NOENT, errbuf); 50381af68beaSAlexander Stetsenko break; 50391af68beaSAlexander Stetsenko default: 50401af68beaSAlexander Stetsenko err = zfs_standard_error_fmt(hdl, errno, errbuf); 50411af68beaSAlexander Stetsenko break; 50421af68beaSAlexander Stetsenko } 50431af68beaSAlexander Stetsenko } 50441af68beaSAlexander Stetsenko 50451af68beaSAlexander Stetsenko free(nvbuf); 50461af68beaSAlexander Stetsenko 50471af68beaSAlexander Stetsenko return (err); 50481af68beaSAlexander Stetsenko } 50491af68beaSAlexander Stetsenko 50501af68beaSAlexander Stetsenko int 50511af68beaSAlexander Stetsenko zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl) 50521af68beaSAlexander Stetsenko { 50533b2aab18SMatthew Ahrens int err; 50543b2aab18SMatthew Ahrens char errbuf[1024]; 50551af68beaSAlexander Stetsenko 50563b2aab18SMatthew Ahrens err = lzc_get_holds(zhp->zfs_name, nvl); 50571af68beaSAlexander Stetsenko 50583b2aab18SMatthew Ahrens if (err != 0) { 50593b2aab18SMatthew Ahrens libzfs_handle_t *hdl = zhp->zfs_hdl; 50601af68beaSAlexander Stetsenko 50611af68beaSAlexander Stetsenko (void) snprintf(errbuf, sizeof (errbuf), 50621af68beaSAlexander Stetsenko dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"), 50633b2aab18SMatthew Ahrens zhp->zfs_name); 50643b2aab18SMatthew Ahrens switch (err) { 50651af68beaSAlexander Stetsenko case ENOTSUP: 50661af68beaSAlexander Stetsenko zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 50671af68beaSAlexander Stetsenko "pool must be upgraded")); 50681af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADVERSION, errbuf); 50691af68beaSAlexander Stetsenko break; 50701af68beaSAlexander Stetsenko case EINVAL: 50711af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADTYPE, errbuf); 50721af68beaSAlexander Stetsenko break; 50731af68beaSAlexander Stetsenko case ENOENT: 50741af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_NOENT, errbuf); 50751af68beaSAlexander Stetsenko break; 50761af68beaSAlexander Stetsenko default: 50771af68beaSAlexander Stetsenko err = zfs_standard_error_fmt(hdl, errno, errbuf); 50781af68beaSAlexander Stetsenko break; 50791af68beaSAlexander Stetsenko } 50801af68beaSAlexander Stetsenko } 50811af68beaSAlexander Stetsenko 50821af68beaSAlexander Stetsenko return (err); 50831af68beaSAlexander Stetsenko } 50841af68beaSAlexander Stetsenko 50853e30c24aSWill Andrews /* 50863e30c24aSWill Andrews * Convert the zvol's volume size to an appropriate reservation. 50873e30c24aSWill Andrews * Note: If this routine is updated, it is necessary to update the ZFS test 50883e30c24aSWill Andrews * suite's shell version in reservation.kshlib. 50893e30c24aSWill Andrews */ 5090c1449561SEric Taylor uint64_t 5091c1449561SEric Taylor zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props) 5092c1449561SEric Taylor { 5093c1449561SEric Taylor uint64_t numdb; 5094c1449561SEric Taylor uint64_t nblocks, volblocksize; 5095c1449561SEric Taylor int ncopies; 5096c1449561SEric Taylor char *strval; 5097c1449561SEric Taylor 5098c1449561SEric Taylor if (nvlist_lookup_string(props, 5099c1449561SEric Taylor zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0) 5100c1449561SEric Taylor ncopies = atoi(strval); 5101c1449561SEric Taylor else 5102c1449561SEric Taylor ncopies = 1; 5103c1449561SEric Taylor if (nvlist_lookup_uint64(props, 5104c1449561SEric Taylor zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 5105c1449561SEric Taylor &volblocksize) != 0) 5106c1449561SEric Taylor volblocksize = ZVOL_DEFAULT_BLOCKSIZE; 5107c1449561SEric Taylor nblocks = volsize/volblocksize; 5108c1449561SEric Taylor /* start with metadnode L0-L6 */ 5109c1449561SEric Taylor numdb = 7; 5110c1449561SEric Taylor /* calculate number of indirects */ 5111c1449561SEric Taylor while (nblocks > 1) { 5112c1449561SEric Taylor nblocks += DNODES_PER_LEVEL - 1; 5113c1449561SEric Taylor nblocks /= DNODES_PER_LEVEL; 5114c1449561SEric Taylor numdb += nblocks; 5115c1449561SEric Taylor } 5116c1449561SEric Taylor numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1); 5117c1449561SEric Taylor volsize *= ncopies; 5118c1449561SEric Taylor /* 5119c1449561SEric Taylor * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't 5120c1449561SEric Taylor * compressed, but in practice they compress down to about 5121c1449561SEric Taylor * 1100 bytes 5122c1449561SEric Taylor */ 5123c1449561SEric Taylor numdb *= 1ULL << DN_MAX_INDBLKSHIFT; 5124c1449561SEric Taylor volsize += numdb; 5125c1449561SEric Taylor return (volsize); 5126c1449561SEric Taylor } 5127