1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51544Seschrock * Common Development and Distribution License (the "License"). 61544Seschrock * You may not use this file except in compliance with the License. 7789Sahrens * 8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9789Sahrens * or http://www.opensolaris.org/os/licensing. 10789Sahrens * See the License for the specific language governing permissions 11789Sahrens * and limitations under the License. 12789Sahrens * 13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15789Sahrens * If applicable, add the following below this CDDL HEADER, with the 16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18789Sahrens * 19789Sahrens * CDDL HEADER END 20789Sahrens */ 213126Sahl 22789Sahrens /* 233363Sgw25295 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 24789Sahrens * Use is subject to license terms. 25789Sahrens */ 26789Sahrens 27789Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 28789Sahrens 29789Sahrens #include <assert.h> 30789Sahrens #include <ctype.h> 31789Sahrens #include <errno.h> 32789Sahrens #include <libdevinfo.h> 33789Sahrens #include <libintl.h> 34789Sahrens #include <math.h> 35789Sahrens #include <stdio.h> 36789Sahrens #include <stdlib.h> 37789Sahrens #include <strings.h> 38789Sahrens #include <unistd.h> 39789Sahrens #include <zone.h> 402082Seschrock #include <fcntl.h> 41789Sahrens #include <sys/mntent.h> 42789Sahrens #include <sys/mnttab.h> 431294Slling #include <sys/mount.h> 444543Smarks #include <sys/avl.h> 454543Smarks #include <priv.h> 464543Smarks #include <pwd.h> 474543Smarks #include <grp.h> 484543Smarks #include <stddef.h> 494543Smarks #include <ucred.h> 50789Sahrens 51789Sahrens #include <sys/spa.h> 52789Sahrens #include <sys/zio.h> 532676Seschrock #include <sys/zap.h> 54789Sahrens #include <libzfs.h> 55789Sahrens 56789Sahrens #include "zfs_namecheck.h" 57789Sahrens #include "zfs_prop.h" 58789Sahrens #include "libzfs_impl.h" 594543Smarks #include "zfs_deleg.h" 60789Sahrens 614490Svb160487 static int create_parents(libzfs_handle_t *, char *, int); 624007Smmusante static int zvol_create_link_common(libzfs_handle_t *, const char *, int); 634007Smmusante 64789Sahrens /* 65789Sahrens * Given a single type (not a mask of types), return the type in a human 66789Sahrens * readable form. 67789Sahrens */ 68789Sahrens const char * 69789Sahrens zfs_type_to_name(zfs_type_t type) 70789Sahrens { 71789Sahrens switch (type) { 72789Sahrens case ZFS_TYPE_FILESYSTEM: 73789Sahrens return (dgettext(TEXT_DOMAIN, "filesystem")); 74789Sahrens case ZFS_TYPE_SNAPSHOT: 75789Sahrens return (dgettext(TEXT_DOMAIN, "snapshot")); 76789Sahrens case ZFS_TYPE_VOLUME: 77789Sahrens return (dgettext(TEXT_DOMAIN, "volume")); 78789Sahrens } 79789Sahrens 80789Sahrens return (NULL); 81789Sahrens } 82789Sahrens 83789Sahrens /* 84789Sahrens * Given a path and mask of ZFS types, return a string describing this dataset. 85789Sahrens * This is used when we fail to open a dataset and we cannot get an exact type. 86789Sahrens * We guess what the type would have been based on the path and the mask of 87789Sahrens * acceptable types. 88789Sahrens */ 89789Sahrens static const char * 90789Sahrens path_to_str(const char *path, int types) 91789Sahrens { 92789Sahrens /* 93789Sahrens * When given a single type, always report the exact type. 94789Sahrens */ 95789Sahrens if (types == ZFS_TYPE_SNAPSHOT) 96789Sahrens return (dgettext(TEXT_DOMAIN, "snapshot")); 97789Sahrens if (types == ZFS_TYPE_FILESYSTEM) 98789Sahrens return (dgettext(TEXT_DOMAIN, "filesystem")); 99789Sahrens if (types == ZFS_TYPE_VOLUME) 100789Sahrens return (dgettext(TEXT_DOMAIN, "volume")); 101789Sahrens 102789Sahrens /* 103789Sahrens * The user is requesting more than one type of dataset. If this is the 104789Sahrens * case, consult the path itself. If we're looking for a snapshot, and 105789Sahrens * a '@' is found, then report it as "snapshot". Otherwise, remove the 106789Sahrens * snapshot attribute and try again. 107789Sahrens */ 108789Sahrens if (types & ZFS_TYPE_SNAPSHOT) { 109789Sahrens if (strchr(path, '@') != NULL) 110789Sahrens return (dgettext(TEXT_DOMAIN, "snapshot")); 111789Sahrens return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT)); 112789Sahrens } 113789Sahrens 114789Sahrens 115789Sahrens /* 116789Sahrens * The user has requested either filesystems or volumes. 117789Sahrens * We have no way of knowing a priori what type this would be, so always 118789Sahrens * report it as "filesystem" or "volume", our two primitive types. 119789Sahrens */ 120789Sahrens if (types & ZFS_TYPE_FILESYSTEM) 121789Sahrens return (dgettext(TEXT_DOMAIN, "filesystem")); 122789Sahrens 123789Sahrens assert(types & ZFS_TYPE_VOLUME); 124789Sahrens return (dgettext(TEXT_DOMAIN, "volume")); 125789Sahrens } 126789Sahrens 127789Sahrens /* 128789Sahrens * Validate a ZFS path. This is used even before trying to open the dataset, to 129789Sahrens * provide a more meaningful error message. We place a more useful message in 130789Sahrens * 'buf' detailing exactly why the name was not valid. 131789Sahrens */ 132789Sahrens static int 1332082Seschrock zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type) 134789Sahrens { 135789Sahrens namecheck_err_t why; 136789Sahrens char what; 137789Sahrens 138789Sahrens if (dataset_namecheck(path, &why, &what) != 0) { 1392082Seschrock if (hdl != NULL) { 140789Sahrens switch (why) { 1411003Slling case NAME_ERR_TOOLONG: 1422082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1432082Seschrock "name is too long")); 1441003Slling break; 1451003Slling 146789Sahrens case NAME_ERR_LEADING_SLASH: 1472082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1482082Seschrock "leading slash in name")); 149789Sahrens break; 150789Sahrens 151789Sahrens case NAME_ERR_EMPTY_COMPONENT: 1522082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1532082Seschrock "empty component in name")); 154789Sahrens break; 155789Sahrens 156789Sahrens case NAME_ERR_TRAILING_SLASH: 1572082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1582082Seschrock "trailing slash in name")); 159789Sahrens break; 160789Sahrens 161789Sahrens case NAME_ERR_INVALCHAR: 1622082Seschrock zfs_error_aux(hdl, 163789Sahrens dgettext(TEXT_DOMAIN, "invalid character " 1642082Seschrock "'%c' in name"), what); 165789Sahrens break; 166789Sahrens 167789Sahrens case NAME_ERR_MULTIPLE_AT: 1682082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1692082Seschrock "multiple '@' delimiters in name")); 170789Sahrens break; 1712856Snd150628 1722856Snd150628 case NAME_ERR_NOLETTER: 1732856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1742856Snd150628 "pool doesn't begin with a letter")); 1752856Snd150628 break; 1762856Snd150628 1772856Snd150628 case NAME_ERR_RESERVED: 1782856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1792856Snd150628 "name is reserved")); 1802856Snd150628 break; 1812856Snd150628 1822856Snd150628 case NAME_ERR_DISKLIKE: 1832856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1842856Snd150628 "reserved disk name")); 1852856Snd150628 break; 186789Sahrens } 187789Sahrens } 188789Sahrens 189789Sahrens return (0); 190789Sahrens } 191789Sahrens 192789Sahrens if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) { 1932082Seschrock if (hdl != NULL) 1942082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1952082Seschrock "snapshot delimiter '@' in filesystem name")); 196789Sahrens return (0); 197789Sahrens } 198789Sahrens 1992199Sahrens if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) { 2002199Sahrens if (hdl != NULL) 2012199Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2023413Smmusante "missing '@' delimiter in snapshot name")); 2032199Sahrens return (0); 2042199Sahrens } 2052199Sahrens 2062082Seschrock return (-1); 207789Sahrens } 208789Sahrens 209789Sahrens int 210789Sahrens zfs_name_valid(const char *name, zfs_type_t type) 211789Sahrens { 2122082Seschrock return (zfs_validate_name(NULL, name, type)); 213789Sahrens } 214789Sahrens 215789Sahrens /* 2162676Seschrock * This function takes the raw DSL properties, and filters out the user-defined 2172676Seschrock * properties into a separate nvlist. 2182676Seschrock */ 2194217Seschrock static nvlist_t * 2204217Seschrock process_user_props(zfs_handle_t *zhp, nvlist_t *props) 2212676Seschrock { 2222676Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 2232676Seschrock nvpair_t *elem; 2242676Seschrock nvlist_t *propval; 2254217Seschrock nvlist_t *nvl; 2264217Seschrock 2274217Seschrock if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) { 2284217Seschrock (void) no_memory(hdl); 2294217Seschrock return (NULL); 2304217Seschrock } 2312676Seschrock 2322676Seschrock elem = NULL; 2334217Seschrock while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 2342676Seschrock if (!zfs_prop_user(nvpair_name(elem))) 2352676Seschrock continue; 2362676Seschrock 2372676Seschrock verify(nvpair_value_nvlist(elem, &propval) == 0); 2384217Seschrock if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) { 2394217Seschrock nvlist_free(nvl); 2404217Seschrock (void) no_memory(hdl); 2414217Seschrock return (NULL); 2424217Seschrock } 2432676Seschrock } 2442676Seschrock 2454217Seschrock return (nvl); 2462676Seschrock } 2472676Seschrock 2482676Seschrock /* 249789Sahrens * Utility function to gather stats (objset and zpl) for the given object. 250789Sahrens */ 251789Sahrens static int 252789Sahrens get_stats(zfs_handle_t *zhp) 253789Sahrens { 254789Sahrens zfs_cmd_t zc = { 0 }; 2552676Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 2564217Seschrock nvlist_t *allprops, *userprops; 257789Sahrens 258789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 259789Sahrens 2602676Seschrock if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) 2612082Seschrock return (-1); 2621356Seschrock 2632082Seschrock while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) { 2641356Seschrock if (errno == ENOMEM) { 2652676Seschrock if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 2662676Seschrock zcmd_free_nvlists(&zc); 2672082Seschrock return (-1); 2682676Seschrock } 2691356Seschrock } else { 2702676Seschrock zcmd_free_nvlists(&zc); 2711356Seschrock return (-1); 2721356Seschrock } 2731356Seschrock } 274789Sahrens 2752885Sahrens zhp->zfs_dmustats = zc.zc_objset_stats; /* structure assignment */ 276789Sahrens 2772676Seschrock (void) strlcpy(zhp->zfs_root, zc.zc_value, sizeof (zhp->zfs_root)); 2781544Seschrock 2794217Seschrock if (zcmd_read_dst_nvlist(hdl, &zc, &allprops) != 0) { 2802676Seschrock zcmd_free_nvlists(&zc); 2812082Seschrock return (-1); 2822082Seschrock } 283789Sahrens 2842676Seschrock zcmd_free_nvlists(&zc); 2852676Seschrock 2864217Seschrock if ((userprops = process_user_props(zhp, allprops)) == NULL) { 2874217Seschrock nvlist_free(allprops); 2882676Seschrock return (-1); 2894217Seschrock } 2904217Seschrock 2914217Seschrock nvlist_free(zhp->zfs_props); 2924217Seschrock nvlist_free(zhp->zfs_user_props); 2934217Seschrock 2944217Seschrock zhp->zfs_props = allprops; 2954217Seschrock zhp->zfs_user_props = userprops; 2962082Seschrock 297789Sahrens return (0); 298789Sahrens } 299789Sahrens 300789Sahrens /* 301789Sahrens * Refresh the properties currently stored in the handle. 302789Sahrens */ 303789Sahrens void 304789Sahrens zfs_refresh_properties(zfs_handle_t *zhp) 305789Sahrens { 306789Sahrens (void) get_stats(zhp); 307789Sahrens } 308789Sahrens 309789Sahrens /* 310789Sahrens * Makes a handle from the given dataset name. Used by zfs_open() and 311789Sahrens * zfs_iter_* to create child handles on the fly. 312789Sahrens */ 313789Sahrens zfs_handle_t * 3142082Seschrock make_dataset_handle(libzfs_handle_t *hdl, const char *path) 315789Sahrens { 3162082Seschrock zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 3174543Smarks char *logstr; 3182082Seschrock 3192082Seschrock if (zhp == NULL) 3202082Seschrock return (NULL); 3212082Seschrock 3222082Seschrock zhp->zfs_hdl = hdl; 323789Sahrens 3244543Smarks /* 3254543Smarks * Preserve history log string. 3264543Smarks * any changes performed here will be 3274543Smarks * logged as an internal event. 3284543Smarks */ 3294543Smarks logstr = zhp->zfs_hdl->libzfs_log_str; 3304543Smarks zhp->zfs_hdl->libzfs_log_str = NULL; 3311758Sahrens top: 332789Sahrens (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name)); 333789Sahrens 334789Sahrens if (get_stats(zhp) != 0) { 3354543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 336789Sahrens free(zhp); 337789Sahrens return (NULL); 338789Sahrens } 339789Sahrens 3401758Sahrens if (zhp->zfs_dmustats.dds_inconsistent) { 3411758Sahrens zfs_cmd_t zc = { 0 }; 3421758Sahrens 3431758Sahrens /* 3441758Sahrens * If it is dds_inconsistent, then we've caught it in 3451758Sahrens * the middle of a 'zfs receive' or 'zfs destroy', and 3461758Sahrens * it is inconsistent from the ZPL's point of view, so 3471758Sahrens * can't be mounted. However, it could also be that we 3481758Sahrens * have crashed in the middle of one of those 3491758Sahrens * operations, in which case we need to get rid of the 3501758Sahrens * inconsistent state. We do that by either rolling 3511758Sahrens * back to the previous snapshot (which will fail if 3521758Sahrens * there is none), or destroying the filesystem. Note 3531758Sahrens * that if we are still in the middle of an active 3541758Sahrens * 'receive' or 'destroy', then the rollback and destroy 3551758Sahrens * will fail with EBUSY and we will drive on as usual. 3561758Sahrens */ 3571758Sahrens 3581758Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3591758Sahrens 3602885Sahrens if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) { 3612082Seschrock (void) zvol_remove_link(hdl, zhp->zfs_name); 3621758Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3631758Sahrens } else { 3641758Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3651758Sahrens } 3661758Sahrens 3671758Sahrens /* If we can successfully roll it back, reget the stats */ 3682082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_ROLLBACK, &zc) == 0) 3691758Sahrens goto top; 3701758Sahrens /* 3711758Sahrens * If we can sucessfully destroy it, pretend that it 3721758Sahrens * never existed. 3731758Sahrens */ 3742082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc) == 0) { 3754543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 3761758Sahrens free(zhp); 3771758Sahrens errno = ENOENT; 3781758Sahrens return (NULL); 3791758Sahrens } 3801758Sahrens } 3811758Sahrens 382789Sahrens /* 383789Sahrens * We've managed to open the dataset and gather statistics. Determine 384789Sahrens * the high-level type. 385789Sahrens */ 3862885Sahrens if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) 3872885Sahrens zhp->zfs_head_type = ZFS_TYPE_VOLUME; 3882885Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) 3892885Sahrens zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM; 3902885Sahrens else 3912885Sahrens abort(); 3922885Sahrens 393789Sahrens if (zhp->zfs_dmustats.dds_is_snapshot) 394789Sahrens zhp->zfs_type = ZFS_TYPE_SNAPSHOT; 395789Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) 396789Sahrens zhp->zfs_type = ZFS_TYPE_VOLUME; 397789Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) 398789Sahrens zhp->zfs_type = ZFS_TYPE_FILESYSTEM; 399789Sahrens else 4002082Seschrock abort(); /* we should never see any other types */ 401789Sahrens 4024543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 403789Sahrens return (zhp); 404789Sahrens } 405789Sahrens 406789Sahrens /* 407789Sahrens * Opens the given snapshot, filesystem, or volume. The 'types' 408789Sahrens * argument is a mask of acceptable types. The function will print an 409789Sahrens * appropriate error message and return NULL if it can't be opened. 410789Sahrens */ 411789Sahrens zfs_handle_t * 4122082Seschrock zfs_open(libzfs_handle_t *hdl, const char *path, int types) 413789Sahrens { 414789Sahrens zfs_handle_t *zhp; 4152082Seschrock char errbuf[1024]; 4162082Seschrock 4172082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 4182082Seschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), path); 419789Sahrens 420789Sahrens /* 4212082Seschrock * Validate the name before we even try to open it. 422789Sahrens */ 423*5094Slling if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET)) { 4242082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4252082Seschrock "invalid dataset name")); 4262082Seschrock (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 427789Sahrens return (NULL); 428789Sahrens } 429789Sahrens 430789Sahrens /* 431789Sahrens * Try to get stats for the dataset, which will tell us if it exists. 432789Sahrens */ 433789Sahrens errno = 0; 4342082Seschrock if ((zhp = make_dataset_handle(hdl, path)) == NULL) { 4353237Slling (void) zfs_standard_error(hdl, errno, errbuf); 436789Sahrens return (NULL); 437789Sahrens } 438789Sahrens 439789Sahrens if (!(types & zhp->zfs_type)) { 4402082Seschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 4412142Seschrock zfs_close(zhp); 442789Sahrens return (NULL); 443789Sahrens } 444789Sahrens 445789Sahrens return (zhp); 446789Sahrens } 447789Sahrens 448789Sahrens /* 449789Sahrens * Release a ZFS handle. Nothing to do but free the associated memory. 450789Sahrens */ 451789Sahrens void 452789Sahrens zfs_close(zfs_handle_t *zhp) 453789Sahrens { 454789Sahrens if (zhp->zfs_mntopts) 455789Sahrens free(zhp->zfs_mntopts); 4562676Seschrock nvlist_free(zhp->zfs_props); 4572676Seschrock nvlist_free(zhp->zfs_user_props); 458789Sahrens free(zhp); 459789Sahrens } 460789Sahrens 4613912Slling 4623912Slling /* 4632676Seschrock * Given an nvlist of properties to set, validates that they are correct, and 4642676Seschrock * parses any numeric properties (index, boolean, etc) if they are specified as 4652676Seschrock * strings. 466789Sahrens */ 467*5094Slling static nvlist_t * 468*5094Slling zfs_validate_properties(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl, 469*5094Slling uint64_t zoned, zfs_handle_t *zhp, const char *errbuf) 470789Sahrens { 4712676Seschrock nvpair_t *elem; 4722676Seschrock uint64_t intval; 4732676Seschrock char *strval; 474*5094Slling zfs_prop_t prop; 4752676Seschrock nvlist_t *ret; 4762676Seschrock 4772676Seschrock if (type == ZFS_TYPE_SNAPSHOT) { 4782676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4793413Smmusante "snapshot properties cannot be modified")); 4802676Seschrock (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 481*5094Slling return (NULL); 482*5094Slling } 483*5094Slling 484*5094Slling if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) { 485*5094Slling (void) no_memory(hdl); 486*5094Slling return (NULL); 487789Sahrens } 488789Sahrens 4892676Seschrock elem = NULL; 4902676Seschrock while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { 491*5094Slling const char *propname = nvpair_name(elem); 4922676Seschrock 4932676Seschrock /* 4942676Seschrock * Make sure this property is valid and applies to this type. 4952676Seschrock */ 496*5094Slling if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) { 497*5094Slling if (!zfs_prop_user(propname)) { 4982676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 499*5094Slling "invalid property '%s'"), propname); 500*5094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 501*5094Slling goto error; 502*5094Slling } 503*5094Slling 504*5094Slling /* 505*5094Slling * If this is a user property, make sure it's a 506*5094Slling * string, and that it's less than ZAP_MAXNAMELEN. 507*5094Slling */ 508*5094Slling if (nvpair_type(elem) != DATA_TYPE_STRING) { 509*5094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 510*5094Slling "'%s' must be a string"), propname); 511*5094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 512*5094Slling goto error; 513*5094Slling } 514*5094Slling 515*5094Slling if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) { 516*5094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 517*5094Slling "property name '%s' is too long"), 5182676Seschrock propname); 5192676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 5202676Seschrock goto error; 5212676Seschrock } 5222676Seschrock 5232676Seschrock (void) nvpair_value_string(elem, &strval); 5242676Seschrock if (nvlist_add_string(ret, propname, strval) != 0) { 5252676Seschrock (void) no_memory(hdl); 5262676Seschrock goto error; 5272676Seschrock } 5282676Seschrock continue; 529789Sahrens } 5302676Seschrock 5312676Seschrock if (!zfs_prop_valid_for_type(prop, type)) { 5322676Seschrock zfs_error_aux(hdl, 5332676Seschrock dgettext(TEXT_DOMAIN, "'%s' does not " 5342676Seschrock "apply to datasets of this type"), propname); 5352676Seschrock (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 5362676Seschrock goto error; 5372676Seschrock } 5382676Seschrock 5392676Seschrock if (zfs_prop_readonly(prop) && 5402676Seschrock (prop != ZFS_PROP_VOLBLOCKSIZE || zhp != NULL)) { 5412676Seschrock zfs_error_aux(hdl, 5422676Seschrock dgettext(TEXT_DOMAIN, "'%s' is readonly"), 5432676Seschrock propname); 5442676Seschrock (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 5452676Seschrock goto error; 5462676Seschrock } 5472676Seschrock 548*5094Slling if (zprop_parse_value(hdl, elem, prop, type, ret, 549*5094Slling &strval, &intval, errbuf) != 0) 5502676Seschrock goto error; 5512676Seschrock 5522676Seschrock /* 5532676Seschrock * Perform some additional checks for specific properties. 5542676Seschrock */ 5552676Seschrock switch (prop) { 5564577Sahrens case ZFS_PROP_VERSION: 5574577Sahrens { 5584577Sahrens int version; 5594577Sahrens 5604577Sahrens if (zhp == NULL) 5614577Sahrens break; 5624577Sahrens version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION); 5634577Sahrens if (intval < version) { 5644577Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5654577Sahrens "Can not downgrade; already at version %u"), 5664577Sahrens version); 5674577Sahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 5684577Sahrens goto error; 5694577Sahrens } 5704577Sahrens break; 5714577Sahrens } 5724577Sahrens 5732676Seschrock case ZFS_PROP_RECORDSIZE: 5742676Seschrock case ZFS_PROP_VOLBLOCKSIZE: 5752676Seschrock /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */ 5762676Seschrock if (intval < SPA_MINBLOCKSIZE || 5772676Seschrock intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) { 5782082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5792676Seschrock "'%s' must be power of 2 from %u " 5802676Seschrock "to %uk"), propname, 5812676Seschrock (uint_t)SPA_MINBLOCKSIZE, 5822676Seschrock (uint_t)SPA_MAXBLOCKSIZE >> 10); 5832676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 5842676Seschrock goto error; 585789Sahrens } 586789Sahrens break; 587789Sahrens 5883126Sahl case ZFS_PROP_SHAREISCSI: 5893126Sahl if (strcmp(strval, "off") != 0 && 5903126Sahl strcmp(strval, "on") != 0 && 5913126Sahl strcmp(strval, "type=disk") != 0) { 5923126Sahl zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5933126Sahl "'%s' must be 'on', 'off', or 'type=disk'"), 5943126Sahl propname); 5953126Sahl (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 5963126Sahl goto error; 5973126Sahl } 5983126Sahl 5993126Sahl break; 6003126Sahl 6012676Seschrock case ZFS_PROP_MOUNTPOINT: 6024778Srm160521 { 6034778Srm160521 namecheck_err_t why; 6044778Srm160521 6052676Seschrock if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 || 6062676Seschrock strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0) 6072676Seschrock break; 6082676Seschrock 6094778Srm160521 if (mountpoint_namecheck(strval, &why)) { 6104778Srm160521 switch (why) { 6114778Srm160521 case NAME_ERR_LEADING_SLASH: 6124778Srm160521 zfs_error_aux(hdl, 6134778Srm160521 dgettext(TEXT_DOMAIN, 6144778Srm160521 "'%s' must be an absolute path, " 6154778Srm160521 "'none', or 'legacy'"), propname); 6164778Srm160521 break; 6174778Srm160521 case NAME_ERR_TOOLONG: 6184778Srm160521 zfs_error_aux(hdl, 6194778Srm160521 dgettext(TEXT_DOMAIN, 6204778Srm160521 "component of '%s' is too long"), 6214778Srm160521 propname); 6224778Srm160521 break; 6234778Srm160521 } 6242676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 6252676Seschrock goto error; 626789Sahrens } 6274778Srm160521 } 6284778Srm160521 6293126Sahl /*FALLTHRU*/ 6303126Sahl 6313126Sahl case ZFS_PROP_SHARENFS: 6323126Sahl /* 6333126Sahl * For the mountpoint and sharenfs properties, check if 6343126Sahl * it can be set in a global/non-global zone based on 6353126Sahl * the zoned property value: 6363126Sahl * 6373126Sahl * global zone non-global zone 6383126Sahl * -------------------------------------------------- 6393126Sahl * zoned=on mountpoint (no) mountpoint (yes) 6403126Sahl * sharenfs (no) sharenfs (no) 6413126Sahl * 6423126Sahl * zoned=off mountpoint (yes) N/A 6433126Sahl * sharenfs (yes) 6443126Sahl */ 6452676Seschrock if (zoned) { 6462676Seschrock if (getzoneid() == GLOBAL_ZONEID) { 6472676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6482676Seschrock "'%s' cannot be set on " 6492676Seschrock "dataset in a non-global zone"), 6502676Seschrock propname); 6512676Seschrock (void) zfs_error(hdl, EZFS_ZONED, 6522676Seschrock errbuf); 6532676Seschrock goto error; 6542676Seschrock } else if (prop == ZFS_PROP_SHARENFS) { 6552676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6562676Seschrock "'%s' cannot be set in " 6572676Seschrock "a non-global zone"), propname); 6582676Seschrock (void) zfs_error(hdl, EZFS_ZONED, 6592676Seschrock errbuf); 6602676Seschrock goto error; 6612676Seschrock } 6622676Seschrock } else if (getzoneid() != GLOBAL_ZONEID) { 6632676Seschrock /* 6642676Seschrock * If zoned property is 'off', this must be in 6652676Seschrock * a globle zone. If not, something is wrong. 6662676Seschrock */ 6672676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6682676Seschrock "'%s' cannot be set while dataset " 6692676Seschrock "'zoned' property is set"), propname); 6702676Seschrock (void) zfs_error(hdl, EZFS_ZONED, errbuf); 6712676Seschrock goto error; 6722676Seschrock } 6733126Sahl 6744180Sdougm /* 6754180Sdougm * At this point, it is legitimate to set the 6764180Sdougm * property. Now we want to make sure that the 6774180Sdougm * property value is valid if it is sharenfs. 6784180Sdougm */ 6794180Sdougm if (prop == ZFS_PROP_SHARENFS && 6804217Seschrock strcmp(strval, "on") != 0 && 6814217Seschrock strcmp(strval, "off") != 0) { 6824180Sdougm 6834180Sdougm /* 6844180Sdougm * Must be an NFS option string so 6854180Sdougm * init the libshare in order to 6864180Sdougm * enable the parser and then parse 6874180Sdougm * the options. We use the control API 6884180Sdougm * since we don't care about the 6894180Sdougm * current configuration and don't 6904180Sdougm * want the overhead of loading it 6914180Sdougm * until we actually do something. 6924180Sdougm */ 6934180Sdougm 6944217Seschrock if (zfs_init_libshare(hdl, 6954217Seschrock SA_INIT_CONTROL_API) != SA_OK) { 6964217Seschrock /* 6974217Seschrock * An error occurred so we can't do 6984217Seschrock * anything 6994217Seschrock */ 7004217Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7014217Seschrock "'%s' cannot be set: problem " 7024217Seschrock "in share initialization"), 7034217Seschrock propname); 7044217Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 7054217Seschrock errbuf); 7064217Seschrock goto error; 7074217Seschrock } 7084217Seschrock 7094217Seschrock if (zfs_parse_options(strval, "nfs") != SA_OK) { 7104217Seschrock /* 7114217Seschrock * There was an error in parsing so 7124217Seschrock * deal with it by issuing an error 7134217Seschrock * message and leaving after 7144217Seschrock * uninitializing the the libshare 7154217Seschrock * interface. 7164217Seschrock */ 7174217Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7184217Seschrock "'%s' cannot be set to invalid " 7194217Seschrock "options"), propname); 7204217Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 7214217Seschrock errbuf); 7224217Seschrock zfs_uninit_libshare(hdl); 7234217Seschrock goto error; 7244217Seschrock } 7254180Sdougm zfs_uninit_libshare(hdl); 7264180Sdougm } 7274180Sdougm 7283126Sahl break; 7292676Seschrock } 7302676Seschrock 7312676Seschrock /* 7322676Seschrock * For changes to existing volumes, we have some additional 7332676Seschrock * checks to enforce. 7342676Seschrock */ 7352676Seschrock if (type == ZFS_TYPE_VOLUME && zhp != NULL) { 7362676Seschrock uint64_t volsize = zfs_prop_get_int(zhp, 7372676Seschrock ZFS_PROP_VOLSIZE); 7382676Seschrock uint64_t blocksize = zfs_prop_get_int(zhp, 7392676Seschrock ZFS_PROP_VOLBLOCKSIZE); 7402676Seschrock char buf[64]; 7412676Seschrock 7422676Seschrock switch (prop) { 7432676Seschrock case ZFS_PROP_RESERVATION: 7442676Seschrock if (intval > volsize) { 7452676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7462676Seschrock "'%s' is greater than current " 7472676Seschrock "volume size"), propname); 7482676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 7492676Seschrock errbuf); 7502676Seschrock goto error; 7512676Seschrock } 7522676Seschrock break; 7532676Seschrock 7542676Seschrock case ZFS_PROP_VOLSIZE: 7552676Seschrock if (intval % blocksize != 0) { 7562676Seschrock zfs_nicenum(blocksize, buf, 7572676Seschrock sizeof (buf)); 7582676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7592676Seschrock "'%s' must be a multiple of " 7602676Seschrock "volume block size (%s)"), 7612676Seschrock propname, buf); 7622676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 7632676Seschrock errbuf); 7642676Seschrock goto error; 7652676Seschrock } 7662676Seschrock 7672676Seschrock if (intval == 0) { 7682676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7692676Seschrock "'%s' cannot be zero"), 7702676Seschrock propname); 7712676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 7722676Seschrock errbuf); 7732676Seschrock goto error; 774789Sahrens } 7753126Sahl break; 776789Sahrens } 777789Sahrens } 778789Sahrens } 779789Sahrens 7802676Seschrock /* 7812676Seschrock * If this is an existing volume, and someone is setting the volsize, 7822676Seschrock * make sure that it matches the reservation, or add it if necessary. 7832676Seschrock */ 7842676Seschrock if (zhp != NULL && type == ZFS_TYPE_VOLUME && 7852676Seschrock nvlist_lookup_uint64(ret, zfs_prop_to_name(ZFS_PROP_VOLSIZE), 7862676Seschrock &intval) == 0) { 7872676Seschrock uint64_t old_volsize = zfs_prop_get_int(zhp, 7882676Seschrock ZFS_PROP_VOLSIZE); 7892676Seschrock uint64_t old_reservation = zfs_prop_get_int(zhp, 7902676Seschrock ZFS_PROP_RESERVATION); 7912676Seschrock uint64_t new_reservation; 7922676Seschrock 7932676Seschrock if (old_volsize == old_reservation && 7942676Seschrock nvlist_lookup_uint64(ret, 7952676Seschrock zfs_prop_to_name(ZFS_PROP_RESERVATION), 7962676Seschrock &new_reservation) != 0) { 7972676Seschrock if (nvlist_add_uint64(ret, 7982676Seschrock zfs_prop_to_name(ZFS_PROP_RESERVATION), 7992676Seschrock intval) != 0) { 8002676Seschrock (void) no_memory(hdl); 8012676Seschrock goto error; 8022676Seschrock } 8032676Seschrock } 8042676Seschrock } 8052676Seschrock 8062676Seschrock return (ret); 8072676Seschrock 8082676Seschrock error: 8092676Seschrock nvlist_free(ret); 8102676Seschrock return (NULL); 811789Sahrens } 812789Sahrens 8134543Smarks static int 8144543Smarks zfs_get_perm_who(const char *who, zfs_deleg_who_type_t *who_type, 8154543Smarks uint64_t *ret_who) 8164543Smarks { 8174543Smarks struct passwd *pwd; 8184543Smarks struct group *grp; 8194543Smarks uid_t id; 8204543Smarks 8214543Smarks if (*who_type == ZFS_DELEG_EVERYONE || *who_type == ZFS_DELEG_CREATE || 8224543Smarks *who_type == ZFS_DELEG_NAMED_SET) { 8234543Smarks *ret_who = -1; 8244543Smarks return (0); 8254543Smarks } 8264543Smarks if (who == NULL && !(*who_type == ZFS_DELEG_EVERYONE)) 8274543Smarks return (EZFS_BADWHO); 8284543Smarks 8294543Smarks if (*who_type == ZFS_DELEG_WHO_UNKNOWN && 8304543Smarks strcmp(who, "everyone") == 0) { 8314543Smarks *ret_who = -1; 8324543Smarks *who_type = ZFS_DELEG_EVERYONE; 8334543Smarks return (0); 8344543Smarks } 8354543Smarks 8364543Smarks pwd = getpwnam(who); 8374543Smarks grp = getgrnam(who); 8384543Smarks 8394543Smarks if ((*who_type == ZFS_DELEG_USER) && pwd) { 8404543Smarks *ret_who = pwd->pw_uid; 8414543Smarks } else if ((*who_type == ZFS_DELEG_GROUP) && grp) { 8424543Smarks *ret_who = grp->gr_gid; 8434543Smarks } else if (pwd) { 8444543Smarks *ret_who = pwd->pw_uid; 8454543Smarks *who_type = ZFS_DELEG_USER; 8464543Smarks } else if (grp) { 8474543Smarks *ret_who = grp->gr_gid; 8484543Smarks *who_type = ZFS_DELEG_GROUP; 8494543Smarks } else { 8504543Smarks char *end; 8514543Smarks 8524543Smarks id = strtol(who, &end, 10); 8534543Smarks if (errno != 0 || *end != '\0') { 8544543Smarks return (EZFS_BADWHO); 8554543Smarks } else { 8564543Smarks *ret_who = id; 8574543Smarks if (*who_type == ZFS_DELEG_WHO_UNKNOWN) 8584543Smarks *who_type = ZFS_DELEG_USER; 8594543Smarks } 8604543Smarks } 8614543Smarks 8624543Smarks return (0); 8634543Smarks } 8644543Smarks 8654543Smarks static void 8664543Smarks zfs_perms_add_to_nvlist(nvlist_t *who_nvp, char *name, nvlist_t *perms_nvp) 8674543Smarks { 8684543Smarks if (perms_nvp != NULL) { 8694543Smarks verify(nvlist_add_nvlist(who_nvp, 8704543Smarks name, perms_nvp) == 0); 8714543Smarks } else { 8724543Smarks verify(nvlist_add_boolean(who_nvp, name) == 0); 8734543Smarks } 8744543Smarks } 8754543Smarks 8764543Smarks static void 8774543Smarks helper(zfs_deleg_who_type_t who_type, uint64_t whoid, char *whostr, 8784543Smarks zfs_deleg_inherit_t inherit, nvlist_t *who_nvp, nvlist_t *perms_nvp, 8794543Smarks nvlist_t *sets_nvp) 8804543Smarks { 8814543Smarks boolean_t do_perms, do_sets; 8824543Smarks char name[ZFS_MAX_DELEG_NAME]; 8834543Smarks 8844543Smarks do_perms = (nvlist_next_nvpair(perms_nvp, NULL) != NULL); 8854543Smarks do_sets = (nvlist_next_nvpair(sets_nvp, NULL) != NULL); 8864543Smarks 8874543Smarks if (!do_perms && !do_sets) 8884543Smarks do_perms = do_sets = B_TRUE; 8894543Smarks 8904543Smarks if (do_perms) { 8914543Smarks zfs_deleg_whokey(name, who_type, inherit, 8924543Smarks (who_type == ZFS_DELEG_NAMED_SET) ? 8934543Smarks whostr : (void *)&whoid); 8944543Smarks zfs_perms_add_to_nvlist(who_nvp, name, perms_nvp); 8954543Smarks } 8964543Smarks if (do_sets) { 8974543Smarks zfs_deleg_whokey(name, toupper(who_type), inherit, 8984543Smarks (who_type == ZFS_DELEG_NAMED_SET) ? 8994543Smarks whostr : (void *)&whoid); 9004543Smarks zfs_perms_add_to_nvlist(who_nvp, name, sets_nvp); 9014543Smarks } 9024543Smarks } 9034543Smarks 9044543Smarks static void 9054543Smarks zfs_perms_add_who_nvlist(nvlist_t *who_nvp, uint64_t whoid, void *whostr, 9064543Smarks nvlist_t *perms_nvp, nvlist_t *sets_nvp, 9074543Smarks zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit) 9084543Smarks { 9094543Smarks if (who_type == ZFS_DELEG_NAMED_SET || who_type == ZFS_DELEG_CREATE) { 9104543Smarks helper(who_type, whoid, whostr, 0, 9114543Smarks who_nvp, perms_nvp, sets_nvp); 9124543Smarks } else { 9134543Smarks if (inherit & ZFS_DELEG_PERM_LOCAL) { 9144543Smarks helper(who_type, whoid, whostr, ZFS_DELEG_LOCAL, 9154543Smarks who_nvp, perms_nvp, sets_nvp); 9164543Smarks } 9174543Smarks if (inherit & ZFS_DELEG_PERM_DESCENDENT) { 9184543Smarks helper(who_type, whoid, whostr, ZFS_DELEG_DESCENDENT, 9194543Smarks who_nvp, perms_nvp, sets_nvp); 9204543Smarks } 9214543Smarks } 9224543Smarks } 9234543Smarks 9244543Smarks /* 9254543Smarks * Construct nvlist to pass down to kernel for setting/removing permissions. 9264543Smarks * 9274543Smarks * The nvlist is constructed as a series of nvpairs with an optional embedded 9284543Smarks * nvlist of permissions to remove or set. The topmost nvpairs are the actual 9294543Smarks * base attribute named stored in the dsl. 9304543Smarks * Arguments: 9314543Smarks * 9324543Smarks * whostr: is a comma separated list of users, groups, or a single set name. 9334543Smarks * whostr may be null for everyone or create perms. 9344543Smarks * who_type: is the type of entry in whostr. Typically this will be 9354543Smarks * ZFS_DELEG_WHO_UNKNOWN. 9364543Smarks * perms: comman separated list of permissions. May be null if user 9374543Smarks * is requested to remove permissions by who. 9384543Smarks * inherit: Specifies the inheritance of the permissions. Will be either 9394543Smarks * ZFS_DELEG_PERM_LOCAL and/or ZFS_DELEG_PERM_DESCENDENT. 9404543Smarks * nvp The constructed nvlist to pass to zfs_perm_set(). 9414543Smarks * The output nvp will look something like this. 9424543Smarks * ul$1234 -> {create ; destroy } 9434543Smarks * Ul$1234 -> { @myset } 9444543Smarks * s-$@myset - { snapshot; checksum; compression } 9454543Smarks */ 9464543Smarks int 9474543Smarks zfs_build_perms(zfs_handle_t *zhp, char *whostr, char *perms, 9484543Smarks zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit, nvlist_t **nvp) 9494543Smarks { 9504543Smarks nvlist_t *who_nvp; 9514543Smarks nvlist_t *perms_nvp = NULL; 9524543Smarks nvlist_t *sets_nvp = NULL; 9534543Smarks char errbuf[1024]; 9544787Sahrens char *who_tok, *perm; 9554543Smarks int error; 9564543Smarks 9574543Smarks *nvp = NULL; 9584543Smarks 9594543Smarks if (perms) { 9604543Smarks if ((error = nvlist_alloc(&perms_nvp, 9614543Smarks NV_UNIQUE_NAME, 0)) != 0) { 9624543Smarks return (1); 9634543Smarks } 9644543Smarks if ((error = nvlist_alloc(&sets_nvp, 9654543Smarks NV_UNIQUE_NAME, 0)) != 0) { 9664543Smarks nvlist_free(perms_nvp); 9674543Smarks return (1); 9684543Smarks } 9694543Smarks } 9704543Smarks 9714543Smarks if ((error = nvlist_alloc(&who_nvp, NV_UNIQUE_NAME, 0)) != 0) { 9724543Smarks if (perms_nvp) 9734543Smarks nvlist_free(perms_nvp); 9744543Smarks if (sets_nvp) 9754543Smarks nvlist_free(sets_nvp); 9764543Smarks return (1); 9774543Smarks } 9784543Smarks 9794543Smarks if (who_type == ZFS_DELEG_NAMED_SET) { 9804543Smarks namecheck_err_t why; 9814543Smarks char what; 9824543Smarks 9834543Smarks if ((error = permset_namecheck(whostr, &why, &what)) != 0) { 9844787Sahrens nvlist_free(who_nvp); 9854787Sahrens if (perms_nvp) 9864787Sahrens nvlist_free(perms_nvp); 9874787Sahrens if (sets_nvp) 9884787Sahrens nvlist_free(sets_nvp); 9894787Sahrens 9904543Smarks switch (why) { 9914543Smarks case NAME_ERR_NO_AT: 9924543Smarks zfs_error_aux(zhp->zfs_hdl, 9934543Smarks dgettext(TEXT_DOMAIN, 9944543Smarks "set definition must begin with an '@' " 9954543Smarks "character")); 9964543Smarks } 9974543Smarks return (zfs_error(zhp->zfs_hdl, 9984543Smarks EZFS_BADPERMSET, whostr)); 9994543Smarks } 10004543Smarks } 10014543Smarks 10024543Smarks /* 10034543Smarks * Build up nvlist(s) of permissions. Two nvlists are maintained. 10044543Smarks * The first nvlist perms_nvp will have normal permissions and the 10054543Smarks * other sets_nvp will have only permssion set names in it. 10064543Smarks */ 10074787Sahrens for (perm = strtok(perms, ","); perm; perm = strtok(NULL, ",")) { 10084787Sahrens const char *perm_canonical = zfs_deleg_canonicalize_perm(perm); 10094787Sahrens 10104787Sahrens if (perm_canonical) { 10114787Sahrens verify(nvlist_add_boolean(perms_nvp, 10124787Sahrens perm_canonical) == 0); 10134787Sahrens } else if (perm[0] == '@') { 10144787Sahrens verify(nvlist_add_boolean(sets_nvp, perm) == 0); 10154787Sahrens } else { 10164787Sahrens nvlist_free(who_nvp); 10174787Sahrens nvlist_free(perms_nvp); 10184787Sahrens nvlist_free(sets_nvp); 10194787Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_BADPERM, perm)); 10204543Smarks } 10214543Smarks } 10224543Smarks 10234543Smarks if (whostr && who_type != ZFS_DELEG_CREATE) { 10244543Smarks who_tok = strtok(whostr, ","); 10254543Smarks if (who_tok == NULL) { 10264543Smarks nvlist_free(who_nvp); 10274787Sahrens if (perms_nvp) 10284787Sahrens nvlist_free(perms_nvp); 10294543Smarks if (sets_nvp) 10304543Smarks nvlist_free(sets_nvp); 10314543Smarks (void) snprintf(errbuf, sizeof (errbuf), 10324543Smarks dgettext(TEXT_DOMAIN, "Who string is NULL"), 10334543Smarks whostr); 10344543Smarks return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf)); 10354543Smarks } 10364543Smarks } 10374543Smarks 10384543Smarks /* 10394543Smarks * Now create the nvlist(s) 10404543Smarks */ 10414543Smarks do { 10424543Smarks uint64_t who_id; 10434543Smarks 10444543Smarks error = zfs_get_perm_who(who_tok, &who_type, 10454543Smarks &who_id); 10464543Smarks if (error) { 10474543Smarks nvlist_free(who_nvp); 10484787Sahrens if (perms_nvp) 10494787Sahrens nvlist_free(perms_nvp); 10504543Smarks if (sets_nvp) 10514543Smarks nvlist_free(sets_nvp); 10524543Smarks (void) snprintf(errbuf, sizeof (errbuf), 10534543Smarks dgettext(TEXT_DOMAIN, 10544543Smarks "Unable to determine uid/gid for " 10554543Smarks "%s "), who_tok); 10564543Smarks return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf)); 10574543Smarks } 10584543Smarks 10594543Smarks /* 10604543Smarks * add entries for both local and descendent when required 10614543Smarks */ 10624543Smarks zfs_perms_add_who_nvlist(who_nvp, who_id, who_tok, 10634543Smarks perms_nvp, sets_nvp, who_type, inherit); 10644543Smarks 10654543Smarks } while (who_tok = strtok(NULL, ",")); 10664543Smarks *nvp = who_nvp; 10674543Smarks return (0); 10684543Smarks } 10694543Smarks 10704543Smarks static int 10714543Smarks zfs_perm_set_common(zfs_handle_t *zhp, nvlist_t *nvp, boolean_t unset) 10724543Smarks { 10734543Smarks zfs_cmd_t zc = { 0 }; 10744543Smarks int error; 10754543Smarks char errbuf[1024]; 10764543Smarks 10774543Smarks (void) snprintf(errbuf, sizeof (errbuf), 10784543Smarks dgettext(TEXT_DOMAIN, "Cannot update 'allows' for '%s'"), 10794543Smarks zhp->zfs_name); 10804543Smarks 1081*5094Slling if (zcmd_write_src_nvlist(zhp->zfs_hdl, &zc, nvp)) 10824543Smarks return (-1); 10834543Smarks 10844543Smarks (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 10854543Smarks zc.zc_perm_action = unset; 10864543Smarks 10874543Smarks error = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_FSACL, &zc); 10884543Smarks if (error && errno == ENOTSUP) { 10894543Smarks (void) snprintf(errbuf, sizeof (errbuf), 10904543Smarks gettext("Pool must be upgraded to use 'allow/unallow'")); 10914543Smarks zcmd_free_nvlists(&zc); 10924543Smarks return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, errbuf)); 10934543Smarks } else if (error) { 10944543Smarks return (zfs_standard_error(zhp->zfs_hdl, errno, errbuf)); 10954543Smarks } 10964543Smarks zcmd_free_nvlists(&zc); 10974543Smarks 10984543Smarks return (error); 10994543Smarks } 11004543Smarks 11014543Smarks int 11024543Smarks zfs_perm_set(zfs_handle_t *zhp, nvlist_t *nvp) 11034543Smarks { 11044543Smarks return (zfs_perm_set_common(zhp, nvp, B_FALSE)); 11054543Smarks } 11064543Smarks 11074543Smarks int 11084543Smarks zfs_perm_remove(zfs_handle_t *zhp, nvlist_t *perms) 11094543Smarks { 11104543Smarks return (zfs_perm_set_common(zhp, perms, B_TRUE)); 11114543Smarks } 11124543Smarks 11134543Smarks static int 11144543Smarks perm_compare(const void *arg1, const void *arg2) 11154543Smarks { 11164543Smarks const zfs_perm_node_t *node1 = arg1; 11174543Smarks const zfs_perm_node_t *node2 = arg2; 11184543Smarks int ret; 11194543Smarks 11204543Smarks ret = strcmp(node1->z_pname, node2->z_pname); 11214543Smarks 11224543Smarks if (ret > 0) 11234543Smarks return (1); 11244543Smarks if (ret < 0) 11254543Smarks return (-1); 11264543Smarks else 11274543Smarks return (0); 11284543Smarks } 11294543Smarks 11304543Smarks static void 11314543Smarks zfs_destroy_perm_tree(avl_tree_t *tree) 11324543Smarks { 11334543Smarks zfs_perm_node_t *permnode; 11344543Smarks void *cookie; 11354543Smarks 11364543Smarks cookie = NULL; 11374543Smarks while ((permnode = avl_destroy_nodes(tree, &cookie)) != NULL) { 11384543Smarks avl_remove(tree, permnode); 11394543Smarks free(permnode); 11404543Smarks } 11414543Smarks } 11424543Smarks 11434543Smarks static void 11444543Smarks zfs_destroy_tree(avl_tree_t *tree) 11454543Smarks { 11464543Smarks zfs_allow_node_t *allownode; 11474543Smarks void *cookie; 11484543Smarks 11494543Smarks cookie = NULL; 11504543Smarks while ((allownode = avl_destroy_nodes(tree, &cookie)) != NULL) { 11514543Smarks zfs_destroy_perm_tree(&allownode->z_localdescend); 11524543Smarks zfs_destroy_perm_tree(&allownode->z_local); 11534543Smarks zfs_destroy_perm_tree(&allownode->z_descend); 11544543Smarks avl_remove(tree, allownode); 11554543Smarks free(allownode); 11564543Smarks } 11574543Smarks } 11584543Smarks 11594543Smarks void 11604543Smarks zfs_free_allows(zfs_allow_t *allow) 11614543Smarks { 11624543Smarks zfs_allow_t *allownext; 11634543Smarks zfs_allow_t *freeallow; 11644543Smarks 11654543Smarks allownext = allow; 11664543Smarks while (allownext) { 11674543Smarks zfs_destroy_tree(&allownext->z_sets); 11684543Smarks zfs_destroy_tree(&allownext->z_crperms); 11694543Smarks zfs_destroy_tree(&allownext->z_user); 11704543Smarks zfs_destroy_tree(&allownext->z_group); 11714543Smarks zfs_destroy_tree(&allownext->z_everyone); 11724543Smarks freeallow = allownext; 11734543Smarks allownext = allownext->z_next; 11744543Smarks free(freeallow); 11754543Smarks } 11764543Smarks } 11774543Smarks 11784543Smarks static zfs_allow_t * 11794543Smarks zfs_alloc_perm_tree(zfs_handle_t *zhp, zfs_allow_t *prev, char *setpoint) 11804543Smarks { 11814543Smarks zfs_allow_t *ptree; 11824543Smarks 11834543Smarks if ((ptree = zfs_alloc(zhp->zfs_hdl, 11844543Smarks sizeof (zfs_allow_t))) == NULL) { 11854543Smarks return (NULL); 11864543Smarks } 11874543Smarks 11884543Smarks (void) strlcpy(ptree->z_setpoint, setpoint, sizeof (ptree->z_setpoint)); 11894543Smarks avl_create(&ptree->z_sets, 11904543Smarks perm_compare, sizeof (zfs_allow_node_t), 11914543Smarks offsetof(zfs_allow_node_t, z_node)); 11924543Smarks avl_create(&ptree->z_crperms, 11934543Smarks perm_compare, sizeof (zfs_allow_node_t), 11944543Smarks offsetof(zfs_allow_node_t, z_node)); 11954543Smarks avl_create(&ptree->z_user, 11964543Smarks perm_compare, sizeof (zfs_allow_node_t), 11974543Smarks offsetof(zfs_allow_node_t, z_node)); 11984543Smarks avl_create(&ptree->z_group, 11994543Smarks perm_compare, sizeof (zfs_allow_node_t), 12004543Smarks offsetof(zfs_allow_node_t, z_node)); 12014543Smarks avl_create(&ptree->z_everyone, 12024543Smarks perm_compare, sizeof (zfs_allow_node_t), 12034543Smarks offsetof(zfs_allow_node_t, z_node)); 12044543Smarks 12054543Smarks if (prev) 12064543Smarks prev->z_next = ptree; 12074543Smarks ptree->z_next = NULL; 12084543Smarks return (ptree); 12094543Smarks } 12104543Smarks 12114543Smarks /* 12124543Smarks * Add permissions to the appropriate AVL permission tree. 12134543Smarks * The appropriate tree may not be the requested tree. 12144543Smarks * For example if ld indicates a local permission, but 12154543Smarks * same permission also exists as a descendent permission 12164543Smarks * then the permission will be removed from the descendent 12174543Smarks * tree and add the the local+descendent tree. 12184543Smarks */ 12194543Smarks static int 12204543Smarks zfs_coalesce_perm(zfs_handle_t *zhp, zfs_allow_node_t *allownode, 12214543Smarks char *perm, char ld) 12224543Smarks { 12234543Smarks zfs_perm_node_t pnode, *permnode, *permnode2; 12244543Smarks zfs_perm_node_t *newnode; 12254543Smarks avl_index_t where, where2; 12264543Smarks avl_tree_t *tree, *altree; 12274543Smarks 12284543Smarks (void) strlcpy(pnode.z_pname, perm, sizeof (pnode.z_pname)); 12294543Smarks 12304543Smarks if (ld == ZFS_DELEG_NA) { 12314543Smarks tree = &allownode->z_localdescend; 12324543Smarks altree = &allownode->z_descend; 12334543Smarks } else if (ld == ZFS_DELEG_LOCAL) { 12344543Smarks tree = &allownode->z_local; 12354543Smarks altree = &allownode->z_descend; 12364543Smarks } else { 12374543Smarks tree = &allownode->z_descend; 12384543Smarks altree = &allownode->z_local; 12394543Smarks } 12404543Smarks permnode = avl_find(tree, &pnode, &where); 12414543Smarks permnode2 = avl_find(altree, &pnode, &where2); 12424543Smarks 12434543Smarks if (permnode2) { 12444543Smarks avl_remove(altree, permnode2); 12454543Smarks free(permnode2); 12464543Smarks if (permnode == NULL) { 12474543Smarks tree = &allownode->z_localdescend; 12484543Smarks } 12494543Smarks } 12504543Smarks 12514543Smarks /* 12524543Smarks * Now insert new permission in either requested location 12534543Smarks * local/descendent or into ld when perm will exist in both. 12544543Smarks */ 12554543Smarks if (permnode == NULL) { 12564543Smarks if ((newnode = zfs_alloc(zhp->zfs_hdl, 12574543Smarks sizeof (zfs_perm_node_t))) == NULL) { 12584543Smarks return (-1); 12594543Smarks } 12604543Smarks *newnode = pnode; 12614543Smarks avl_add(tree, newnode); 12624543Smarks } 12634543Smarks return (0); 12644543Smarks } 12654577Sahrens 12664543Smarks /* 12674543Smarks * Uggh, this is going to be a bit complicated. 12684543Smarks * we have an nvlist coming out of the kernel that 12694543Smarks * will indicate where the permission is set and then 12704543Smarks * it will contain allow of the various "who's", and what 12714543Smarks * their permissions are. To further complicate this 12724543Smarks * we will then have to coalesce the local,descendent 12734543Smarks * and local+descendent permissions where appropriate. 12744543Smarks * The kernel only knows about a permission as being local 12754543Smarks * or descendent, but not both. 12764543Smarks * 12774543Smarks * In order to make this easier for zfs_main to deal with 12784543Smarks * a series of AVL trees will be used to maintain 12794543Smarks * all of this, primarily for sorting purposes as well 12804543Smarks * as the ability to quickly locate a specific entry. 12814543Smarks * 12824543Smarks * What we end up with are tree's for sets, create perms, 12834543Smarks * user, groups and everyone. With each of those trees 12844543Smarks * we have subtrees for local, descendent and local+descendent 12854543Smarks * permissions. 12864543Smarks */ 12874543Smarks int 12884543Smarks zfs_perm_get(zfs_handle_t *zhp, zfs_allow_t **zfs_perms) 12894543Smarks { 12904543Smarks zfs_cmd_t zc = { 0 }; 12914543Smarks int error; 12924543Smarks nvlist_t *nvlist; 12934543Smarks nvlist_t *permnv, *sourcenv; 12944543Smarks nvpair_t *who_pair, *source_pair; 12954543Smarks nvpair_t *perm_pair; 12964543Smarks char errbuf[1024]; 12974543Smarks zfs_allow_t *zallowp, *newallowp; 12984543Smarks char ld; 12994543Smarks char *nvpname; 13004543Smarks uid_t uid; 13014543Smarks gid_t gid; 13024543Smarks avl_tree_t *tree; 13034543Smarks avl_index_t where; 13044543Smarks 13054543Smarks (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 13064543Smarks 13074543Smarks if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 13084543Smarks return (-1); 13094543Smarks 13104543Smarks while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) { 13114543Smarks if (errno == ENOMEM) { 13124543Smarks if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, &zc) != 0) { 13134543Smarks zcmd_free_nvlists(&zc); 13144543Smarks return (-1); 13154543Smarks } 13164543Smarks } else if (errno == ENOTSUP) { 13174543Smarks zcmd_free_nvlists(&zc); 13184543Smarks (void) snprintf(errbuf, sizeof (errbuf), 13194543Smarks gettext("Pool must be upgraded to use 'allow'")); 13204543Smarks return (zfs_error(zhp->zfs_hdl, 13214543Smarks EZFS_BADVERSION, errbuf)); 13224543Smarks } else { 13234543Smarks zcmd_free_nvlists(&zc); 13244543Smarks return (-1); 13254543Smarks } 13264543Smarks } 13274543Smarks 13284543Smarks if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &nvlist) != 0) { 13294543Smarks zcmd_free_nvlists(&zc); 13304543Smarks return (-1); 13314543Smarks } 13324543Smarks 13334543Smarks zcmd_free_nvlists(&zc); 13344543Smarks 13354543Smarks source_pair = nvlist_next_nvpair(nvlist, NULL); 13364543Smarks 13374543Smarks if (source_pair == NULL) { 13384543Smarks *zfs_perms = NULL; 13394543Smarks return (0); 13404543Smarks } 13414543Smarks 13424543Smarks *zfs_perms = zfs_alloc_perm_tree(zhp, NULL, nvpair_name(source_pair)); 13434543Smarks if (*zfs_perms == NULL) { 13444543Smarks return (0); 13454543Smarks } 13464543Smarks 13474543Smarks zallowp = *zfs_perms; 13484543Smarks 13494543Smarks for (;;) { 13504543Smarks struct passwd *pwd; 13514543Smarks struct group *grp; 13524543Smarks zfs_allow_node_t *allownode; 13534543Smarks zfs_allow_node_t findallownode; 13544543Smarks zfs_allow_node_t *newallownode; 13554543Smarks 13564543Smarks (void) strlcpy(zallowp->z_setpoint, 13574543Smarks nvpair_name(source_pair), 13584543Smarks sizeof (zallowp->z_setpoint)); 13594543Smarks 13604543Smarks if ((error = nvpair_value_nvlist(source_pair, &sourcenv)) != 0) 13614543Smarks goto abort; 13624543Smarks 13634543Smarks /* 13644543Smarks * Make sure nvlist is composed correctly 13654543Smarks */ 13664543Smarks if (zfs_deleg_verify_nvlist(sourcenv)) { 13674543Smarks goto abort; 13684543Smarks } 13694543Smarks 13704543Smarks who_pair = nvlist_next_nvpair(sourcenv, NULL); 13714543Smarks if (who_pair == NULL) { 13724543Smarks goto abort; 13734543Smarks } 13744543Smarks 13754543Smarks do { 13764543Smarks error = nvpair_value_nvlist(who_pair, &permnv); 13774543Smarks if (error) { 13784543Smarks goto abort; 13794543Smarks } 13804543Smarks 13814543Smarks /* 13824543Smarks * First build up the key to use 13834543Smarks * for looking up in the various 13844543Smarks * who trees. 13854543Smarks */ 13864543Smarks ld = nvpair_name(who_pair)[1]; 13874543Smarks nvpname = nvpair_name(who_pair); 13884543Smarks switch (nvpair_name(who_pair)[0]) { 13894543Smarks case ZFS_DELEG_USER: 13904543Smarks case ZFS_DELEG_USER_SETS: 13914543Smarks tree = &zallowp->z_user; 13924543Smarks uid = atol(&nvpname[3]); 13934543Smarks pwd = getpwuid(uid); 13944543Smarks (void) snprintf(findallownode.z_key, 13954543Smarks sizeof (findallownode.z_key), "user %s", 13964543Smarks (pwd) ? pwd->pw_name : 13974543Smarks &nvpair_name(who_pair)[3]); 13984543Smarks break; 13994543Smarks case ZFS_DELEG_GROUP: 14004543Smarks case ZFS_DELEG_GROUP_SETS: 14014543Smarks tree = &zallowp->z_group; 14024543Smarks gid = atol(&nvpname[3]); 14034543Smarks grp = getgrgid(gid); 14044543Smarks (void) snprintf(findallownode.z_key, 14054543Smarks sizeof (findallownode.z_key), "group %s", 14064543Smarks (grp) ? grp->gr_name : 14074543Smarks &nvpair_name(who_pair)[3]); 14084543Smarks break; 14094543Smarks case ZFS_DELEG_CREATE: 14104543Smarks case ZFS_DELEG_CREATE_SETS: 14114543Smarks tree = &zallowp->z_crperms; 14124543Smarks (void) strlcpy(findallownode.z_key, "", 14134543Smarks sizeof (findallownode.z_key)); 14144543Smarks break; 14154543Smarks case ZFS_DELEG_EVERYONE: 14164543Smarks case ZFS_DELEG_EVERYONE_SETS: 14174543Smarks (void) snprintf(findallownode.z_key, 14184543Smarks sizeof (findallownode.z_key), "everyone"); 14194543Smarks tree = &zallowp->z_everyone; 14204543Smarks break; 14214543Smarks case ZFS_DELEG_NAMED_SET: 14224543Smarks case ZFS_DELEG_NAMED_SET_SETS: 14234543Smarks (void) snprintf(findallownode.z_key, 14244543Smarks sizeof (findallownode.z_key), "%s", 14254543Smarks &nvpair_name(who_pair)[3]); 14264543Smarks tree = &zallowp->z_sets; 14274543Smarks break; 14284543Smarks } 14294543Smarks 14304543Smarks /* 14314543Smarks * Place who in tree 14324543Smarks */ 14334543Smarks allownode = avl_find(tree, &findallownode, &where); 14344543Smarks if (allownode == NULL) { 14354543Smarks if ((newallownode = zfs_alloc(zhp->zfs_hdl, 14364543Smarks sizeof (zfs_allow_node_t))) == NULL) { 14374543Smarks goto abort; 14384543Smarks } 14394543Smarks avl_create(&newallownode->z_localdescend, 14404543Smarks perm_compare, 14414543Smarks sizeof (zfs_perm_node_t), 14424543Smarks offsetof(zfs_perm_node_t, z_node)); 14434543Smarks avl_create(&newallownode->z_local, 14444543Smarks perm_compare, 14454543Smarks sizeof (zfs_perm_node_t), 14464543Smarks offsetof(zfs_perm_node_t, z_node)); 14474543Smarks avl_create(&newallownode->z_descend, 14484543Smarks perm_compare, 14494543Smarks sizeof (zfs_perm_node_t), 14504543Smarks offsetof(zfs_perm_node_t, z_node)); 14514543Smarks (void) strlcpy(newallownode->z_key, 14524543Smarks findallownode.z_key, 14534543Smarks sizeof (findallownode.z_key)); 14544543Smarks avl_insert(tree, newallownode, where); 14554543Smarks allownode = newallownode; 14564543Smarks } 14574543Smarks 14584543Smarks /* 14594543Smarks * Now iterate over the permissions and 14604543Smarks * place them in the appropriate local, 14614543Smarks * descendent or local+descendent tree. 14624543Smarks * 14634543Smarks * The permissions are added to the tree 14644543Smarks * via zfs_coalesce_perm(). 14654543Smarks */ 14664543Smarks perm_pair = nvlist_next_nvpair(permnv, NULL); 14674543Smarks if (perm_pair == NULL) 14684543Smarks goto abort; 14694543Smarks do { 14704543Smarks if (zfs_coalesce_perm(zhp, allownode, 14714543Smarks nvpair_name(perm_pair), ld) != 0) 14724543Smarks goto abort; 14734543Smarks } while (perm_pair = nvlist_next_nvpair(permnv, 14744543Smarks perm_pair)); 14754543Smarks } while (who_pair = nvlist_next_nvpair(sourcenv, who_pair)); 14764543Smarks 14774543Smarks source_pair = nvlist_next_nvpair(nvlist, source_pair); 14784543Smarks if (source_pair == NULL) 14794543Smarks break; 14804543Smarks 14814543Smarks /* 14824543Smarks * allocate another node from the link list of 14834543Smarks * zfs_allow_t structures 14844543Smarks */ 14854543Smarks newallowp = zfs_alloc_perm_tree(zhp, zallowp, 14864543Smarks nvpair_name(source_pair)); 14874543Smarks if (newallowp == NULL) { 14884543Smarks goto abort; 14894543Smarks } 14904543Smarks zallowp = newallowp; 14914543Smarks } 14924543Smarks nvlist_free(nvlist); 14934543Smarks return (0); 14944543Smarks abort: 14954543Smarks zfs_free_allows(*zfs_perms); 14964543Smarks nvlist_free(nvlist); 14974543Smarks return (-1); 14984543Smarks } 14994543Smarks 1500789Sahrens /* 1501789Sahrens * Given a property name and value, set the property for the given dataset. 1502789Sahrens */ 1503789Sahrens int 15042676Seschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval) 1505789Sahrens { 1506789Sahrens zfs_cmd_t zc = { 0 }; 15072676Seschrock int ret = -1; 15082676Seschrock prop_changelist_t *cl = NULL; 15092082Seschrock char errbuf[1024]; 15102082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 15112676Seschrock nvlist_t *nvl = NULL, *realprops; 15122676Seschrock zfs_prop_t prop; 15132082Seschrock 15142082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 15152676Seschrock dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 15162082Seschrock zhp->zfs_name); 15172082Seschrock 15182676Seschrock if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 || 15192676Seschrock nvlist_add_string(nvl, propname, propval) != 0) { 15202676Seschrock (void) no_memory(hdl); 15212676Seschrock goto error; 1522789Sahrens } 1523789Sahrens 1524*5094Slling if ((realprops = zfs_validate_properties(hdl, zhp->zfs_type, nvl, 15252676Seschrock zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL) 15262676Seschrock goto error; 1527*5094Slling 15282676Seschrock nvlist_free(nvl); 15292676Seschrock nvl = realprops; 15302676Seschrock 15312676Seschrock prop = zfs_name_to_prop(propname); 15322676Seschrock 1533789Sahrens if ((cl = changelist_gather(zhp, prop, 0)) == NULL) 15342676Seschrock goto error; 1535789Sahrens 1536789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 15372082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 15382082Seschrock "child dataset with inherited mountpoint is used " 15392082Seschrock "in a non-global zone")); 15402082Seschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1541789Sahrens goto error; 1542789Sahrens } 1543789Sahrens 1544789Sahrens if ((ret = changelist_prefix(cl)) != 0) 1545789Sahrens goto error; 1546789Sahrens 1547789Sahrens /* 1548789Sahrens * Execute the corresponding ioctl() to set this property. 1549789Sahrens */ 1550789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1551789Sahrens 1552*5094Slling if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0) 15532676Seschrock goto error; 15542676Seschrock 15554543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc); 1556789Sahrens 1557789Sahrens if (ret != 0) { 1558789Sahrens switch (errno) { 1559789Sahrens 1560789Sahrens case ENOSPC: 1561789Sahrens /* 1562789Sahrens * For quotas and reservations, ENOSPC indicates 1563789Sahrens * something different; setting a quota or reservation 1564789Sahrens * doesn't use any disk space. 1565789Sahrens */ 1566789Sahrens switch (prop) { 1567789Sahrens case ZFS_PROP_QUOTA: 15682082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 15692082Seschrock "size is less than current used or " 15702082Seschrock "reserved space")); 15712082Seschrock (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 1572789Sahrens break; 1573789Sahrens 1574789Sahrens case ZFS_PROP_RESERVATION: 15752082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 15762082Seschrock "size is greater than available space")); 15772082Seschrock (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 1578789Sahrens break; 1579789Sahrens 1580789Sahrens default: 15812082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 1582789Sahrens break; 1583789Sahrens } 1584789Sahrens break; 1585789Sahrens 1586789Sahrens case EBUSY: 15872082Seschrock if (prop == ZFS_PROP_VOLBLOCKSIZE) 15882082Seschrock (void) zfs_error(hdl, EZFS_VOLHASDATA, errbuf); 15892082Seschrock else 15902676Seschrock (void) zfs_standard_error(hdl, EBUSY, errbuf); 1591789Sahrens break; 1592789Sahrens 15931175Slling case EROFS: 15942082Seschrock (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf); 15951175Slling break; 15961175Slling 15973886Sahl case ENOTSUP: 15983886Sahl zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 15994603Sahrens "pool must be upgraded to set this " 16004603Sahrens "property or value")); 16013886Sahl (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 16023886Sahl break; 16033886Sahl 1604789Sahrens case EOVERFLOW: 1605789Sahrens /* 1606789Sahrens * This platform can't address a volume this big. 1607789Sahrens */ 1608789Sahrens #ifdef _ILP32 1609789Sahrens if (prop == ZFS_PROP_VOLSIZE) { 16102082Seschrock (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf); 1611789Sahrens break; 1612789Sahrens } 1613789Sahrens #endif 16142082Seschrock /* FALLTHROUGH */ 1615789Sahrens default: 16162082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 1617789Sahrens } 1618789Sahrens } else { 1619789Sahrens /* 1620789Sahrens * Refresh the statistics so the new property value 1621789Sahrens * is reflected. 1622789Sahrens */ 16232676Seschrock if ((ret = changelist_postfix(cl)) == 0) 16242676Seschrock (void) get_stats(zhp); 1625789Sahrens } 1626789Sahrens 1627789Sahrens error: 16282676Seschrock nvlist_free(nvl); 16292676Seschrock zcmd_free_nvlists(&zc); 16302676Seschrock if (cl) 16312676Seschrock changelist_free(cl); 1632789Sahrens return (ret); 1633789Sahrens } 1634789Sahrens 1635789Sahrens /* 1636789Sahrens * Given a property, inherit the value from the parent dataset. 1637789Sahrens */ 1638789Sahrens int 16392676Seschrock zfs_prop_inherit(zfs_handle_t *zhp, const char *propname) 1640789Sahrens { 1641789Sahrens zfs_cmd_t zc = { 0 }; 1642789Sahrens int ret; 1643789Sahrens prop_changelist_t *cl; 16442082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 16452082Seschrock char errbuf[1024]; 16462676Seschrock zfs_prop_t prop; 16472082Seschrock 16482082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 16492082Seschrock "cannot inherit %s for '%s'"), propname, zhp->zfs_name); 1650789Sahrens 1651*5094Slling if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) { 16522676Seschrock /* 16532676Seschrock * For user properties, the amount of work we have to do is very 16542676Seschrock * small, so just do it here. 16552676Seschrock */ 16562676Seschrock if (!zfs_prop_user(propname)) { 16572676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 16582676Seschrock "invalid property")); 16592676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 16602676Seschrock } 16612676Seschrock 16622676Seschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 16632676Seschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 16642676Seschrock 16654849Sahrens if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0) 16662676Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 16672676Seschrock 16682676Seschrock return (0); 16692676Seschrock } 16702676Seschrock 1671789Sahrens /* 1672789Sahrens * Verify that this property is inheritable. 1673789Sahrens */ 16742082Seschrock if (zfs_prop_readonly(prop)) 16752082Seschrock return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf)); 16762082Seschrock 16772082Seschrock if (!zfs_prop_inheritable(prop)) 16782082Seschrock return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf)); 1679789Sahrens 1680789Sahrens /* 1681789Sahrens * Check to see if the value applies to this type 1682789Sahrens */ 16832082Seschrock if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 16842082Seschrock return (zfs_error(hdl, EZFS_PROPTYPE, errbuf)); 1685789Sahrens 16863443Srm160521 /* 16873443Srm160521 * Normalize the name, to get rid of shorthand abbrevations. 16883443Srm160521 */ 16893443Srm160521 propname = zfs_prop_to_name(prop); 1690789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 16912676Seschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 1692789Sahrens 1693789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID && 1694789Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 16952082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 16962082Seschrock "dataset is used in a non-global zone")); 16972082Seschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 1698789Sahrens } 1699789Sahrens 1700789Sahrens /* 1701789Sahrens * Determine datasets which will be affected by this change, if any. 1702789Sahrens */ 1703789Sahrens if ((cl = changelist_gather(zhp, prop, 0)) == NULL) 1704789Sahrens return (-1); 1705789Sahrens 1706789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 17072082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 17082082Seschrock "child dataset with inherited mountpoint is used " 17092082Seschrock "in a non-global zone")); 17102082Seschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1711789Sahrens goto error; 1712789Sahrens } 1713789Sahrens 1714789Sahrens if ((ret = changelist_prefix(cl)) != 0) 1715789Sahrens goto error; 1716789Sahrens 17174849Sahrens if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) { 17182082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 1719789Sahrens } else { 1720789Sahrens 17212169Snd150628 if ((ret = changelist_postfix(cl)) != 0) 1722789Sahrens goto error; 1723789Sahrens 1724789Sahrens /* 1725789Sahrens * Refresh the statistics so the new property is reflected. 1726789Sahrens */ 1727789Sahrens (void) get_stats(zhp); 1728789Sahrens } 1729789Sahrens 1730789Sahrens error: 1731789Sahrens changelist_free(cl); 1732789Sahrens return (ret); 1733789Sahrens } 1734789Sahrens 1735789Sahrens /* 17361356Seschrock * True DSL properties are stored in an nvlist. The following two functions 17371356Seschrock * extract them appropriately. 17381356Seschrock */ 17391356Seschrock static uint64_t 17401356Seschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 17411356Seschrock { 17421356Seschrock nvlist_t *nv; 17431356Seschrock uint64_t value; 17441356Seschrock 17452885Sahrens *source = NULL; 17461356Seschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 17471356Seschrock zfs_prop_to_name(prop), &nv) == 0) { 1748*5094Slling verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0); 1749*5094Slling (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 17501356Seschrock } else { 17511356Seschrock value = zfs_prop_default_numeric(prop); 17521356Seschrock *source = ""; 17531356Seschrock } 17541356Seschrock 17551356Seschrock return (value); 17561356Seschrock } 17571356Seschrock 17581356Seschrock static char * 17591356Seschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 17601356Seschrock { 17611356Seschrock nvlist_t *nv; 17621356Seschrock char *value; 17631356Seschrock 17642885Sahrens *source = NULL; 17651356Seschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 17661356Seschrock zfs_prop_to_name(prop), &nv) == 0) { 1767*5094Slling verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0); 1768*5094Slling (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 17691356Seschrock } else { 17701356Seschrock if ((value = (char *)zfs_prop_default_string(prop)) == NULL) 17711356Seschrock value = ""; 17721356Seschrock *source = ""; 17731356Seschrock } 17741356Seschrock 17751356Seschrock return (value); 17761356Seschrock } 17771356Seschrock 17781356Seschrock /* 1779789Sahrens * Internal function for getting a numeric property. Both zfs_prop_get() and 1780789Sahrens * zfs_prop_get_int() are built using this interface. 1781789Sahrens * 1782789Sahrens * Certain properties can be overridden using 'mount -o'. In this case, scan 1783789Sahrens * the contents of the /etc/mnttab entry, searching for the appropriate options. 1784789Sahrens * If they differ from the on-disk values, report the current values and mark 1785789Sahrens * the source "temporary". 1786789Sahrens */ 17872082Seschrock static int 1788*5094Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src, 17892082Seschrock char **source, uint64_t *val) 1790789Sahrens { 1791789Sahrens struct mnttab mnt; 17923265Sahrens char *mntopt_on = NULL; 17933265Sahrens char *mntopt_off = NULL; 1794789Sahrens 1795789Sahrens *source = NULL; 1796789Sahrens 17973265Sahrens switch (prop) { 17983265Sahrens case ZFS_PROP_ATIME: 17993265Sahrens mntopt_on = MNTOPT_ATIME; 18003265Sahrens mntopt_off = MNTOPT_NOATIME; 18013265Sahrens break; 18023265Sahrens 18033265Sahrens case ZFS_PROP_DEVICES: 18043265Sahrens mntopt_on = MNTOPT_DEVICES; 18053265Sahrens mntopt_off = MNTOPT_NODEVICES; 18063265Sahrens break; 18073265Sahrens 18083265Sahrens case ZFS_PROP_EXEC: 18093265Sahrens mntopt_on = MNTOPT_EXEC; 18103265Sahrens mntopt_off = MNTOPT_NOEXEC; 18113265Sahrens break; 18123265Sahrens 18133265Sahrens case ZFS_PROP_READONLY: 18143265Sahrens mntopt_on = MNTOPT_RO; 18153265Sahrens mntopt_off = MNTOPT_RW; 18163265Sahrens break; 18173265Sahrens 18183265Sahrens case ZFS_PROP_SETUID: 18193265Sahrens mntopt_on = MNTOPT_SETUID; 18203265Sahrens mntopt_off = MNTOPT_NOSETUID; 18213265Sahrens break; 18223265Sahrens 18233265Sahrens case ZFS_PROP_XATTR: 18243265Sahrens mntopt_on = MNTOPT_XATTR; 18253265Sahrens mntopt_off = MNTOPT_NOXATTR; 18263265Sahrens break; 18273265Sahrens } 18283265Sahrens 18292474Seschrock /* 18302474Seschrock * Because looking up the mount options is potentially expensive 18312474Seschrock * (iterating over all of /etc/mnttab), we defer its calculation until 18322474Seschrock * we're looking up a property which requires its presence. 18332474Seschrock */ 18342474Seschrock if (!zhp->zfs_mntcheck && 18353265Sahrens (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) { 18363265Sahrens struct mnttab entry, search = { 0 }; 18373265Sahrens FILE *mnttab = zhp->zfs_hdl->libzfs_mnttab; 18382474Seschrock 18392474Seschrock search.mnt_special = (char *)zhp->zfs_name; 18402474Seschrock search.mnt_fstype = MNTTYPE_ZFS; 18413265Sahrens rewind(mnttab); 18423265Sahrens 18433265Sahrens if (getmntany(mnttab, &entry, &search) == 0) { 18443265Sahrens zhp->zfs_mntopts = zfs_strdup(zhp->zfs_hdl, 18453265Sahrens entry.mnt_mntopts); 18463265Sahrens if (zhp->zfs_mntopts == NULL) 18473265Sahrens return (-1); 18483265Sahrens } 18492474Seschrock 18502474Seschrock zhp->zfs_mntcheck = B_TRUE; 18512474Seschrock } 18522474Seschrock 1853789Sahrens if (zhp->zfs_mntopts == NULL) 1854789Sahrens mnt.mnt_mntopts = ""; 1855789Sahrens else 1856789Sahrens mnt.mnt_mntopts = zhp->zfs_mntopts; 1857789Sahrens 1858789Sahrens switch (prop) { 1859789Sahrens case ZFS_PROP_ATIME: 18603265Sahrens case ZFS_PROP_DEVICES: 18613265Sahrens case ZFS_PROP_EXEC: 18623265Sahrens case ZFS_PROP_READONLY: 18633265Sahrens case ZFS_PROP_SETUID: 18643265Sahrens case ZFS_PROP_XATTR: 18652082Seschrock *val = getprop_uint64(zhp, prop, source); 18662082Seschrock 18673265Sahrens if (hasmntopt(&mnt, mntopt_on) && !*val) { 18682082Seschrock *val = B_TRUE; 1869789Sahrens if (src) 1870*5094Slling *src = ZPROP_SRC_TEMPORARY; 18713265Sahrens } else if (hasmntopt(&mnt, mntopt_off) && *val) { 18722082Seschrock *val = B_FALSE; 1873789Sahrens if (src) 1874*5094Slling *src = ZPROP_SRC_TEMPORARY; 1875789Sahrens } 18762082Seschrock break; 1877789Sahrens 18783265Sahrens case ZFS_PROP_CANMOUNT: 18792082Seschrock *val = getprop_uint64(zhp, prop, source); 18803417Srm160521 if (*val == 0) 18813417Srm160521 *source = zhp->zfs_name; 18823417Srm160521 else 18833417Srm160521 *source = ""; /* default */ 18842082Seschrock break; 1885789Sahrens 1886789Sahrens case ZFS_PROP_QUOTA: 1887789Sahrens case ZFS_PROP_RESERVATION: 18882885Sahrens *val = getprop_uint64(zhp, prop, source); 18892885Sahrens if (*val == 0) 1890789Sahrens *source = ""; /* default */ 1891789Sahrens else 1892789Sahrens *source = zhp->zfs_name; 18932082Seschrock break; 1894789Sahrens 1895789Sahrens case ZFS_PROP_MOUNTED: 18962082Seschrock *val = (zhp->zfs_mntopts != NULL); 18972082Seschrock break; 1898789Sahrens 18993377Seschrock case ZFS_PROP_NUMCLONES: 19003377Seschrock *val = zhp->zfs_dmustats.dds_num_clones; 19013377Seschrock break; 19023377Seschrock 1903789Sahrens default: 19044577Sahrens switch (zfs_prop_get_type(prop)) { 19054787Sahrens case PROP_TYPE_NUMBER: 19064787Sahrens case PROP_TYPE_INDEX: 19074577Sahrens *val = getprop_uint64(zhp, prop, source); 19084577Sahrens break; 19094577Sahrens 19104787Sahrens case PROP_TYPE_STRING: 19114577Sahrens default: 19124577Sahrens zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 19134577Sahrens "cannot get non-numeric property")); 19144577Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP, 19154577Sahrens dgettext(TEXT_DOMAIN, "internal error"))); 19164577Sahrens } 1917789Sahrens } 1918789Sahrens 1919789Sahrens return (0); 1920789Sahrens } 1921789Sahrens 1922789Sahrens /* 1923789Sahrens * Calculate the source type, given the raw source string. 1924789Sahrens */ 1925789Sahrens static void 1926*5094Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source, 1927789Sahrens char *statbuf, size_t statlen) 1928789Sahrens { 1929*5094Slling if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY) 1930789Sahrens return; 1931789Sahrens 1932789Sahrens if (source == NULL) { 1933*5094Slling *srctype = ZPROP_SRC_NONE; 1934789Sahrens } else if (source[0] == '\0') { 1935*5094Slling *srctype = ZPROP_SRC_DEFAULT; 1936789Sahrens } else { 1937789Sahrens if (strcmp(source, zhp->zfs_name) == 0) { 1938*5094Slling *srctype = ZPROP_SRC_LOCAL; 1939789Sahrens } else { 1940789Sahrens (void) strlcpy(statbuf, source, statlen); 1941*5094Slling *srctype = ZPROP_SRC_INHERITED; 1942789Sahrens } 1943789Sahrens } 1944789Sahrens 1945789Sahrens } 1946789Sahrens 1947789Sahrens /* 1948789Sahrens * Retrieve a property from the given object. If 'literal' is specified, then 1949789Sahrens * numbers are left as exact values. Otherwise, numbers are converted to a 1950789Sahrens * human-readable form. 1951789Sahrens * 1952789Sahrens * Returns 0 on success, or -1 on error. 1953789Sahrens */ 1954789Sahrens int 1955789Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen, 1956*5094Slling zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal) 1957789Sahrens { 1958789Sahrens char *source = NULL; 1959789Sahrens uint64_t val; 1960789Sahrens char *str; 1961789Sahrens const char *root; 19622676Seschrock const char *strval; 1963789Sahrens 1964789Sahrens /* 1965789Sahrens * Check to see if this property applies to our object 1966789Sahrens */ 1967789Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 1968789Sahrens return (-1); 1969789Sahrens 1970789Sahrens if (src) 1971*5094Slling *src = ZPROP_SRC_NONE; 1972789Sahrens 1973789Sahrens switch (prop) { 1974789Sahrens case ZFS_PROP_CREATION: 1975789Sahrens /* 1976789Sahrens * 'creation' is a time_t stored in the statistics. We convert 1977789Sahrens * this into a string unless 'literal' is specified. 1978789Sahrens */ 1979789Sahrens { 19802885Sahrens val = getprop_uint64(zhp, prop, &source); 19812885Sahrens time_t time = (time_t)val; 1982789Sahrens struct tm t; 1983789Sahrens 1984789Sahrens if (literal || 1985789Sahrens localtime_r(&time, &t) == NULL || 1986789Sahrens strftime(propbuf, proplen, "%a %b %e %k:%M %Y", 1987789Sahrens &t) == 0) 19882885Sahrens (void) snprintf(propbuf, proplen, "%llu", val); 1989789Sahrens } 1990789Sahrens break; 1991789Sahrens 1992789Sahrens case ZFS_PROP_MOUNTPOINT: 1993789Sahrens /* 1994789Sahrens * Getting the precise mountpoint can be tricky. 1995789Sahrens * 1996789Sahrens * - for 'none' or 'legacy', return those values. 1997789Sahrens * - for default mountpoints, construct it as /zfs/<dataset> 1998789Sahrens * - for inherited mountpoints, we want to take everything 1999789Sahrens * after our ancestor and append it to the inherited value. 2000789Sahrens * 2001789Sahrens * If the pool has an alternate root, we want to prepend that 2002789Sahrens * root to any values we return. 2003789Sahrens */ 20041544Seschrock root = zhp->zfs_root; 20051356Seschrock str = getprop_string(zhp, prop, &source); 20061356Seschrock 20071356Seschrock if (str[0] == '\0') { 2008789Sahrens (void) snprintf(propbuf, proplen, "%s/zfs/%s", 2009789Sahrens root, zhp->zfs_name); 20101356Seschrock } else if (str[0] == '/') { 20111356Seschrock const char *relpath = zhp->zfs_name + strlen(source); 2012789Sahrens 2013789Sahrens if (relpath[0] == '/') 2014789Sahrens relpath++; 20151356Seschrock if (str[1] == '\0') 20161356Seschrock str++; 2017789Sahrens 2018789Sahrens if (relpath[0] == '\0') 2019789Sahrens (void) snprintf(propbuf, proplen, "%s%s", 20201356Seschrock root, str); 2021789Sahrens else 2022789Sahrens (void) snprintf(propbuf, proplen, "%s%s%s%s", 20231356Seschrock root, str, relpath[0] == '@' ? "" : "/", 2024789Sahrens relpath); 2025789Sahrens } else { 2026789Sahrens /* 'legacy' or 'none' */ 20271356Seschrock (void) strlcpy(propbuf, str, proplen); 2028789Sahrens } 2029789Sahrens 2030789Sahrens break; 2031789Sahrens 2032789Sahrens case ZFS_PROP_ORIGIN: 20332885Sahrens (void) strlcpy(propbuf, getprop_string(zhp, prop, &source), 2034789Sahrens proplen); 2035789Sahrens /* 2036789Sahrens * If there is no parent at all, return failure to indicate that 2037789Sahrens * it doesn't apply to this dataset. 2038789Sahrens */ 2039789Sahrens if (propbuf[0] == '\0') 2040789Sahrens return (-1); 2041789Sahrens break; 2042789Sahrens 2043789Sahrens case ZFS_PROP_QUOTA: 2044789Sahrens case ZFS_PROP_RESERVATION: 20452082Seschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 20462082Seschrock return (-1); 2047789Sahrens 2048789Sahrens /* 2049789Sahrens * If quota or reservation is 0, we translate this into 'none' 2050789Sahrens * (unless literal is set), and indicate that it's the default 2051789Sahrens * value. Otherwise, we print the number nicely and indicate 2052789Sahrens * that its set locally. 2053789Sahrens */ 2054789Sahrens if (val == 0) { 2055789Sahrens if (literal) 2056789Sahrens (void) strlcpy(propbuf, "0", proplen); 2057789Sahrens else 2058789Sahrens (void) strlcpy(propbuf, "none", proplen); 2059789Sahrens } else { 2060789Sahrens if (literal) 20612856Snd150628 (void) snprintf(propbuf, proplen, "%llu", 20623912Slling (u_longlong_t)val); 2063789Sahrens else 2064789Sahrens zfs_nicenum(val, propbuf, proplen); 2065789Sahrens } 2066789Sahrens break; 2067789Sahrens 2068789Sahrens case ZFS_PROP_COMPRESSRATIO: 20692082Seschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 20702082Seschrock return (-1); 20712856Snd150628 (void) snprintf(propbuf, proplen, "%lld.%02lldx", (longlong_t) 20722856Snd150628 val / 100, (longlong_t)val % 100); 2073789Sahrens break; 2074789Sahrens 2075789Sahrens case ZFS_PROP_TYPE: 2076789Sahrens switch (zhp->zfs_type) { 2077789Sahrens case ZFS_TYPE_FILESYSTEM: 2078789Sahrens str = "filesystem"; 2079789Sahrens break; 2080789Sahrens case ZFS_TYPE_VOLUME: 2081789Sahrens str = "volume"; 2082789Sahrens break; 2083789Sahrens case ZFS_TYPE_SNAPSHOT: 2084789Sahrens str = "snapshot"; 2085789Sahrens break; 2086789Sahrens default: 20872082Seschrock abort(); 2088789Sahrens } 2089789Sahrens (void) snprintf(propbuf, proplen, "%s", str); 2090789Sahrens break; 2091789Sahrens 2092789Sahrens case ZFS_PROP_MOUNTED: 2093789Sahrens /* 2094789Sahrens * The 'mounted' property is a pseudo-property that described 2095789Sahrens * whether the filesystem is currently mounted. Even though 2096789Sahrens * it's a boolean value, the typical values of "on" and "off" 2097789Sahrens * don't make sense, so we translate to "yes" and "no". 2098789Sahrens */ 20992082Seschrock if (get_numeric_property(zhp, ZFS_PROP_MOUNTED, 21002082Seschrock src, &source, &val) != 0) 21012082Seschrock return (-1); 21022082Seschrock if (val) 2103789Sahrens (void) strlcpy(propbuf, "yes", proplen); 2104789Sahrens else 2105789Sahrens (void) strlcpy(propbuf, "no", proplen); 2106789Sahrens break; 2107789Sahrens 2108789Sahrens case ZFS_PROP_NAME: 2109789Sahrens /* 2110789Sahrens * The 'name' property is a pseudo-property derived from the 2111789Sahrens * dataset name. It is presented as a real property to simplify 2112789Sahrens * consumers. 2113789Sahrens */ 2114789Sahrens (void) strlcpy(propbuf, zhp->zfs_name, proplen); 2115789Sahrens break; 2116789Sahrens 2117789Sahrens default: 21184577Sahrens switch (zfs_prop_get_type(prop)) { 21194787Sahrens case PROP_TYPE_NUMBER: 21204577Sahrens if (get_numeric_property(zhp, prop, src, 21214577Sahrens &source, &val) != 0) 21224577Sahrens return (-1); 21234577Sahrens if (literal) 21244577Sahrens (void) snprintf(propbuf, proplen, "%llu", 21254577Sahrens (u_longlong_t)val); 21264577Sahrens else 21274577Sahrens zfs_nicenum(val, propbuf, proplen); 21284577Sahrens break; 21294577Sahrens 21304787Sahrens case PROP_TYPE_STRING: 21314577Sahrens (void) strlcpy(propbuf, 21324577Sahrens getprop_string(zhp, prop, &source), proplen); 21334577Sahrens break; 21344577Sahrens 21354787Sahrens case PROP_TYPE_INDEX: 21364861Sahrens if (get_numeric_property(zhp, prop, src, 21374861Sahrens &source, &val) != 0) 21384861Sahrens return (-1); 21394861Sahrens if (zfs_prop_index_to_string(prop, val, &strval) != 0) 21404577Sahrens return (-1); 21414577Sahrens (void) strlcpy(propbuf, strval, proplen); 21424577Sahrens break; 21434577Sahrens 21444577Sahrens default: 21454577Sahrens abort(); 21464577Sahrens } 2147789Sahrens } 2148789Sahrens 2149789Sahrens get_source(zhp, src, source, statbuf, statlen); 2150789Sahrens 2151789Sahrens return (0); 2152789Sahrens } 2153789Sahrens 2154789Sahrens /* 2155789Sahrens * Utility function to get the given numeric property. Does no validation that 2156789Sahrens * the given property is the appropriate type; should only be used with 2157789Sahrens * hard-coded property types. 2158789Sahrens */ 2159789Sahrens uint64_t 2160789Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop) 2161789Sahrens { 2162789Sahrens char *source; 2163*5094Slling zprop_source_t sourcetype = ZPROP_SRC_NONE; 21642082Seschrock uint64_t val; 21652082Seschrock 21662082Seschrock (void) get_numeric_property(zhp, prop, &sourcetype, &source, &val); 21672082Seschrock 21682082Seschrock return (val); 2169789Sahrens } 2170789Sahrens 2171789Sahrens /* 2172789Sahrens * Similar to zfs_prop_get(), but returns the value as an integer. 2173789Sahrens */ 2174789Sahrens int 2175789Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value, 2176*5094Slling zprop_source_t *src, char *statbuf, size_t statlen) 2177789Sahrens { 2178789Sahrens char *source; 2179789Sahrens 2180789Sahrens /* 2181789Sahrens * Check to see if this property applies to our object 2182789Sahrens */ 21834849Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) { 21843237Slling return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE, 21852082Seschrock dgettext(TEXT_DOMAIN, "cannot get property '%s'"), 21862082Seschrock zfs_prop_to_name(prop))); 21874849Sahrens } 2188789Sahrens 2189789Sahrens if (src) 2190*5094Slling *src = ZPROP_SRC_NONE; 2191789Sahrens 21922082Seschrock if (get_numeric_property(zhp, prop, src, &source, value) != 0) 21932082Seschrock return (-1); 2194789Sahrens 2195789Sahrens get_source(zhp, src, source, statbuf, statlen); 2196789Sahrens 2197789Sahrens return (0); 2198789Sahrens } 2199789Sahrens 2200789Sahrens /* 2201789Sahrens * Returns the name of the given zfs handle. 2202789Sahrens */ 2203789Sahrens const char * 2204789Sahrens zfs_get_name(const zfs_handle_t *zhp) 2205789Sahrens { 2206789Sahrens return (zhp->zfs_name); 2207789Sahrens } 2208789Sahrens 2209789Sahrens /* 2210789Sahrens * Returns the type of the given zfs handle. 2211789Sahrens */ 2212789Sahrens zfs_type_t 2213789Sahrens zfs_get_type(const zfs_handle_t *zhp) 2214789Sahrens { 2215789Sahrens return (zhp->zfs_type); 2216789Sahrens } 2217789Sahrens 2218789Sahrens /* 22191356Seschrock * Iterate over all child filesystems 2220789Sahrens */ 2221789Sahrens int 22221356Seschrock zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data) 2223789Sahrens { 2224789Sahrens zfs_cmd_t zc = { 0 }; 2225789Sahrens zfs_handle_t *nzhp; 2226789Sahrens int ret; 2227789Sahrens 2228789Sahrens for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 22292082Seschrock ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_DATASET_LIST_NEXT, &zc) == 0; 2230789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) { 2231789Sahrens /* 2232789Sahrens * Ignore private dataset names. 2233789Sahrens */ 2234789Sahrens if (dataset_name_hidden(zc.zc_name)) 2235789Sahrens continue; 2236789Sahrens 2237789Sahrens /* 2238789Sahrens * Silently ignore errors, as the only plausible explanation is 2239789Sahrens * that the pool has since been removed. 2240789Sahrens */ 22412082Seschrock if ((nzhp = make_dataset_handle(zhp->zfs_hdl, 22422082Seschrock zc.zc_name)) == NULL) 2243789Sahrens continue; 2244789Sahrens 2245789Sahrens if ((ret = func(nzhp, data)) != 0) 2246789Sahrens return (ret); 2247789Sahrens } 2248789Sahrens 2249789Sahrens /* 2250789Sahrens * An errno value of ESRCH indicates normal completion. If ENOENT is 2251789Sahrens * returned, then the underlying dataset has been removed since we 2252789Sahrens * obtained the handle. 2253789Sahrens */ 2254789Sahrens if (errno != ESRCH && errno != ENOENT) 22552082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 22562082Seschrock dgettext(TEXT_DOMAIN, "cannot iterate filesystems"))); 2257789Sahrens 22581356Seschrock return (0); 22591356Seschrock } 22601356Seschrock 22611356Seschrock /* 22621356Seschrock * Iterate over all snapshots 22631356Seschrock */ 22641356Seschrock int 22651356Seschrock zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data) 22661356Seschrock { 22671356Seschrock zfs_cmd_t zc = { 0 }; 22681356Seschrock zfs_handle_t *nzhp; 22691356Seschrock int ret; 2270789Sahrens 2271789Sahrens for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 22722082Seschrock ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT, 22732082Seschrock &zc) == 0; 2274789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) { 2275789Sahrens 22762082Seschrock if ((nzhp = make_dataset_handle(zhp->zfs_hdl, 22772082Seschrock zc.zc_name)) == NULL) 2278789Sahrens continue; 2279789Sahrens 2280789Sahrens if ((ret = func(nzhp, data)) != 0) 2281789Sahrens return (ret); 2282789Sahrens } 2283789Sahrens 2284789Sahrens /* 2285789Sahrens * An errno value of ESRCH indicates normal completion. If ENOENT is 2286789Sahrens * returned, then the underlying dataset has been removed since we 2287789Sahrens * obtained the handle. Silently ignore this case, and return success. 2288789Sahrens */ 2289789Sahrens if (errno != ESRCH && errno != ENOENT) 22902082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 22912082Seschrock dgettext(TEXT_DOMAIN, "cannot iterate filesystems"))); 2292789Sahrens 2293789Sahrens return (0); 2294789Sahrens } 2295789Sahrens 2296789Sahrens /* 22971356Seschrock * Iterate over all children, snapshots and filesystems 22981356Seschrock */ 22991356Seschrock int 23001356Seschrock zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data) 23011356Seschrock { 23021356Seschrock int ret; 23031356Seschrock 23041356Seschrock if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0) 23051356Seschrock return (ret); 23061356Seschrock 23071356Seschrock return (zfs_iter_snapshots(zhp, func, data)); 23081356Seschrock } 23091356Seschrock 23101356Seschrock /* 2311789Sahrens * Given a complete name, return just the portion that refers to the parent. 2312789Sahrens * Can return NULL if this is a pool. 2313789Sahrens */ 2314789Sahrens static int 2315789Sahrens parent_name(const char *path, char *buf, size_t buflen) 2316789Sahrens { 2317789Sahrens char *loc; 2318789Sahrens 2319789Sahrens if ((loc = strrchr(path, '/')) == NULL) 2320789Sahrens return (-1); 2321789Sahrens 2322789Sahrens (void) strncpy(buf, path, MIN(buflen, loc - path)); 2323789Sahrens buf[loc - path] = '\0'; 2324789Sahrens 2325789Sahrens return (0); 2326789Sahrens } 2327789Sahrens 2328789Sahrens /* 23294490Svb160487 * If accept_ancestor is false, then check to make sure that the given path has 23304490Svb160487 * a parent, and that it exists. If accept_ancestor is true, then find the 23314490Svb160487 * closest existing ancestor for the given path. In prefixlen return the 23324490Svb160487 * length of already existing prefix of the given path. We also fetch the 23334490Svb160487 * 'zoned' property, which is used to validate property settings when creating 23344490Svb160487 * new datasets. 2335789Sahrens */ 2336789Sahrens static int 23374490Svb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned, 23384490Svb160487 boolean_t accept_ancestor, int *prefixlen) 2339789Sahrens { 2340789Sahrens zfs_cmd_t zc = { 0 }; 2341789Sahrens char parent[ZFS_MAXNAMELEN]; 2342789Sahrens char *slash; 23431356Seschrock zfs_handle_t *zhp; 23442082Seschrock char errbuf[1024]; 23452082Seschrock 23462082Seschrock (void) snprintf(errbuf, sizeof (errbuf), "cannot create '%s'", 23472082Seschrock path); 2348789Sahrens 2349789Sahrens /* get parent, and check to see if this is just a pool */ 2350789Sahrens if (parent_name(path, parent, sizeof (parent)) != 0) { 23512082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 23522082Seschrock "missing dataset name")); 23532082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2354789Sahrens } 2355789Sahrens 2356789Sahrens /* check to see if the pool exists */ 2357789Sahrens if ((slash = strchr(parent, '/')) == NULL) 2358789Sahrens slash = parent + strlen(parent); 2359789Sahrens (void) strncpy(zc.zc_name, parent, slash - parent); 2360789Sahrens zc.zc_name[slash - parent] = '\0'; 23612082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 && 2362789Sahrens errno == ENOENT) { 23632082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 23642082Seschrock "no such pool '%s'"), zc.zc_name); 23652082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2366789Sahrens } 2367789Sahrens 2368789Sahrens /* check to see if the parent dataset exists */ 23694490Svb160487 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) { 23704490Svb160487 if (errno == ENOENT && accept_ancestor) { 23714490Svb160487 /* 23724490Svb160487 * Go deeper to find an ancestor, give up on top level. 23734490Svb160487 */ 23744490Svb160487 if (parent_name(parent, parent, sizeof (parent)) != 0) { 23754490Svb160487 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 23764490Svb160487 "no such pool '%s'"), zc.zc_name); 23774490Svb160487 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 23784490Svb160487 } 23794490Svb160487 } else if (errno == ENOENT) { 23802082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 23812082Seschrock "parent does not exist")); 23822082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 23834490Svb160487 } else 23842082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 2385789Sahrens } 2386789Sahrens 23872676Seschrock *zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); 2388789Sahrens /* we are in a non-global zone, but parent is in the global zone */ 23892676Seschrock if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) { 23902082Seschrock (void) zfs_standard_error(hdl, EPERM, errbuf); 23911356Seschrock zfs_close(zhp); 2392789Sahrens return (-1); 2393789Sahrens } 2394789Sahrens 2395789Sahrens /* make sure parent is a filesystem */ 23961356Seschrock if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) { 23972082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 23982082Seschrock "parent is not a filesystem")); 23992082Seschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 24001356Seschrock zfs_close(zhp); 2401789Sahrens return (-1); 2402789Sahrens } 2403789Sahrens 24041356Seschrock zfs_close(zhp); 24054490Svb160487 if (prefixlen != NULL) 24064490Svb160487 *prefixlen = strlen(parent); 24074490Svb160487 return (0); 24084490Svb160487 } 24094490Svb160487 24104490Svb160487 /* 24114490Svb160487 * Finds whether the dataset of the given type(s) exists. 24124490Svb160487 */ 24134490Svb160487 boolean_t 24144490Svb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types) 24154490Svb160487 { 24164490Svb160487 zfs_handle_t *zhp; 24174490Svb160487 24184490Svb160487 if (!zfs_validate_name(hdl, path, types)) 24194490Svb160487 return (B_FALSE); 24204490Svb160487 24214490Svb160487 /* 24224490Svb160487 * Try to get stats for the dataset, which will tell us if it exists. 24234490Svb160487 */ 24244490Svb160487 if ((zhp = make_dataset_handle(hdl, path)) != NULL) { 24254490Svb160487 int ds_type = zhp->zfs_type; 24264490Svb160487 24274490Svb160487 zfs_close(zhp); 24284490Svb160487 if (types & ds_type) 24294490Svb160487 return (B_TRUE); 24304490Svb160487 } 24314490Svb160487 return (B_FALSE); 24324490Svb160487 } 24334490Svb160487 24344490Svb160487 /* 24354490Svb160487 * Creates non-existing ancestors of the given path. 24364490Svb160487 */ 24374490Svb160487 int 24384490Svb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path) 24394490Svb160487 { 24404490Svb160487 int prefix; 24414490Svb160487 uint64_t zoned; 24424490Svb160487 char *path_copy; 24434490Svb160487 int rc; 24444490Svb160487 24454490Svb160487 if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0) 24464490Svb160487 return (-1); 24474490Svb160487 24484490Svb160487 if ((path_copy = strdup(path)) != NULL) { 24494490Svb160487 rc = create_parents(hdl, path_copy, prefix); 24504490Svb160487 free(path_copy); 24514490Svb160487 } 24524490Svb160487 if (path_copy == NULL || rc != 0) 24534490Svb160487 return (-1); 24544490Svb160487 2455789Sahrens return (0); 2456789Sahrens } 2457789Sahrens 2458789Sahrens /* 24592676Seschrock * Create a new filesystem or volume. 2460789Sahrens */ 2461789Sahrens int 24622082Seschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type, 24632676Seschrock nvlist_t *props) 2464789Sahrens { 2465789Sahrens zfs_cmd_t zc = { 0 }; 2466789Sahrens int ret; 2467789Sahrens uint64_t size = 0; 2468789Sahrens uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 24692082Seschrock char errbuf[1024]; 24702676Seschrock uint64_t zoned; 24712082Seschrock 24722082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 24732082Seschrock "cannot create '%s'"), path); 2474789Sahrens 2475789Sahrens /* validate the path, taking care to note the extended error message */ 24762082Seschrock if (!zfs_validate_name(hdl, path, type)) 24772082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2478789Sahrens 2479789Sahrens /* validate parents exist */ 24804490Svb160487 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0) 2481789Sahrens return (-1); 2482789Sahrens 2483789Sahrens /* 2484789Sahrens * The failure modes when creating a dataset of a different type over 2485789Sahrens * one that already exists is a little strange. In particular, if you 2486789Sahrens * try to create a dataset on top of an existing dataset, the ioctl() 2487789Sahrens * will return ENOENT, not EEXIST. To prevent this from happening, we 2488789Sahrens * first try to see if the dataset exists. 2489789Sahrens */ 2490789Sahrens (void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name)); 2491*5094Slling if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) { 24922082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 24932082Seschrock "dataset already exists")); 24942082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 2495789Sahrens } 2496789Sahrens 2497789Sahrens if (type == ZFS_TYPE_VOLUME) 2498789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 2499789Sahrens else 2500789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 2501789Sahrens 2502*5094Slling if (props && (props = zfs_validate_properties(hdl, type, props, 25033912Slling zoned, NULL, errbuf)) == 0) 25042676Seschrock return (-1); 25052676Seschrock 2506789Sahrens if (type == ZFS_TYPE_VOLUME) { 25071133Seschrock /* 25081133Seschrock * If we are creating a volume, the size and block size must 25091133Seschrock * satisfy a few restraints. First, the blocksize must be a 25101133Seschrock * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the 25111133Seschrock * volsize must be a multiple of the block size, and cannot be 25121133Seschrock * zero. 25131133Seschrock */ 25142676Seschrock if (props == NULL || nvlist_lookup_uint64(props, 25152676Seschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) { 25162676Seschrock nvlist_free(props); 25172082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 25182676Seschrock "missing volume size")); 25192676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 2520789Sahrens } 2521789Sahrens 25222676Seschrock if ((ret = nvlist_lookup_uint64(props, 25232676Seschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 25242676Seschrock &blocksize)) != 0) { 25252676Seschrock if (ret == ENOENT) { 25262676Seschrock blocksize = zfs_prop_default_numeric( 25272676Seschrock ZFS_PROP_VOLBLOCKSIZE); 25282676Seschrock } else { 25292676Seschrock nvlist_free(props); 25302676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 25312676Seschrock "missing volume block size")); 25322676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 25332676Seschrock } 25342676Seschrock } 25352676Seschrock 25362676Seschrock if (size == 0) { 25372676Seschrock nvlist_free(props); 25382082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 25392676Seschrock "volume size cannot be zero")); 25402676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 25411133Seschrock } 25421133Seschrock 25431133Seschrock if (size % blocksize != 0) { 25442676Seschrock nvlist_free(props); 25452082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 25462676Seschrock "volume size must be a multiple of volume block " 25472676Seschrock "size")); 25482676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 25491133Seschrock } 2550789Sahrens } 2551789Sahrens 2552*5094Slling if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0) 25532676Seschrock return (-1); 25542676Seschrock nvlist_free(props); 25552676Seschrock 2556789Sahrens /* create the dataset */ 25574543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc); 2558789Sahrens 25593912Slling if (ret == 0 && type == ZFS_TYPE_VOLUME) { 25602082Seschrock ret = zvol_create_link(hdl, path); 25613912Slling if (ret) { 25623912Slling (void) zfs_standard_error(hdl, errno, 25633912Slling dgettext(TEXT_DOMAIN, 25643912Slling "Volume successfully created, but device links " 25653912Slling "were not created")); 25663912Slling zcmd_free_nvlists(&zc); 25673912Slling return (-1); 25683912Slling } 25693912Slling } 2570789Sahrens 25712676Seschrock zcmd_free_nvlists(&zc); 25722676Seschrock 2573789Sahrens /* check for failure */ 2574789Sahrens if (ret != 0) { 2575789Sahrens char parent[ZFS_MAXNAMELEN]; 2576789Sahrens (void) parent_name(path, parent, sizeof (parent)); 2577789Sahrens 2578789Sahrens switch (errno) { 2579789Sahrens case ENOENT: 25802082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 25812082Seschrock "no such parent '%s'"), parent); 25822082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2583789Sahrens 2584789Sahrens case EINVAL: 25852082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 25863413Smmusante "parent '%s' is not a filesystem"), parent); 25872082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 2588789Sahrens 2589789Sahrens case EDOM: 25902082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 25912676Seschrock "volume block size must be power of 2 from " 25922676Seschrock "%u to %uk"), 2593789Sahrens (uint_t)SPA_MINBLOCKSIZE, 2594789Sahrens (uint_t)SPA_MAXBLOCKSIZE >> 10); 25952082Seschrock 25962676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 25972082Seschrock 25984603Sahrens case ENOTSUP: 25994603Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26004603Sahrens "pool must be upgraded to set this " 26014603Sahrens "property or value")); 26024603Sahrens return (zfs_error(hdl, EZFS_BADVERSION, errbuf)); 26034603Sahrens 2604789Sahrens #ifdef _ILP32 2605789Sahrens case EOVERFLOW: 2606789Sahrens /* 2607789Sahrens * This platform can't address a volume this big. 2608789Sahrens */ 26092082Seschrock if (type == ZFS_TYPE_VOLUME) 26102082Seschrock return (zfs_error(hdl, EZFS_VOLTOOBIG, 26112082Seschrock errbuf)); 2612789Sahrens #endif 26132082Seschrock /* FALLTHROUGH */ 2614789Sahrens default: 26152082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 2616789Sahrens } 2617789Sahrens } 2618789Sahrens 2619789Sahrens return (0); 2620789Sahrens } 2621789Sahrens 2622789Sahrens /* 2623789Sahrens * Destroys the given dataset. The caller must make sure that the filesystem 2624789Sahrens * isn't mounted, and that there are no active dependents. 2625789Sahrens */ 2626789Sahrens int 2627789Sahrens zfs_destroy(zfs_handle_t *zhp) 2628789Sahrens { 2629789Sahrens zfs_cmd_t zc = { 0 }; 2630789Sahrens 2631789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2632789Sahrens 26332676Seschrock if (ZFS_IS_VOLUME(zhp)) { 26343126Sahl /* 26354543Smarks * If user doesn't have permissions to unshare volume, then 26364543Smarks * abort the request. This would only happen for a 26374543Smarks * non-privileged user. 26383126Sahl */ 26394543Smarks if (zfs_unshare_iscsi(zhp) != 0) { 26404543Smarks return (-1); 26414543Smarks } 26423126Sahl 26432082Seschrock if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) 2644789Sahrens return (-1); 2645789Sahrens 2646789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 2647789Sahrens } else { 2648789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 2649789Sahrens } 2650789Sahrens 26514543Smarks if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) { 26523237Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, errno, 26532082Seschrock dgettext(TEXT_DOMAIN, "cannot destroy '%s'"), 26542082Seschrock zhp->zfs_name)); 26552199Sahrens } 2656789Sahrens 2657789Sahrens remove_mountpoint(zhp); 2658789Sahrens 2659789Sahrens return (0); 2660789Sahrens } 2661789Sahrens 26622199Sahrens struct destroydata { 26632199Sahrens char *snapname; 26642199Sahrens boolean_t gotone; 26653265Sahrens boolean_t closezhp; 26662199Sahrens }; 26672199Sahrens 26682199Sahrens static int 26692199Sahrens zfs_remove_link_cb(zfs_handle_t *zhp, void *arg) 26702199Sahrens { 26712199Sahrens struct destroydata *dd = arg; 26722199Sahrens zfs_handle_t *szhp; 26732199Sahrens char name[ZFS_MAXNAMELEN]; 26743265Sahrens boolean_t closezhp = dd->closezhp; 26753265Sahrens int rv; 26762199Sahrens 26772676Seschrock (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 26782676Seschrock (void) strlcat(name, "@", sizeof (name)); 26792676Seschrock (void) strlcat(name, dd->snapname, sizeof (name)); 26802199Sahrens 26812199Sahrens szhp = make_dataset_handle(zhp->zfs_hdl, name); 26822199Sahrens if (szhp) { 26832199Sahrens dd->gotone = B_TRUE; 26842199Sahrens zfs_close(szhp); 26852199Sahrens } 26862199Sahrens 26872199Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 26882199Sahrens (void) zvol_remove_link(zhp->zfs_hdl, name); 26892199Sahrens /* 26902199Sahrens * NB: this is simply a best-effort. We don't want to 26912199Sahrens * return an error, because then we wouldn't visit all 26922199Sahrens * the volumes. 26932199Sahrens */ 26942199Sahrens } 26952199Sahrens 26963265Sahrens dd->closezhp = B_TRUE; 26973265Sahrens rv = zfs_iter_filesystems(zhp, zfs_remove_link_cb, arg); 26983265Sahrens if (closezhp) 26993265Sahrens zfs_close(zhp); 27003265Sahrens return (rv); 27012199Sahrens } 27022199Sahrens 27032199Sahrens /* 27042199Sahrens * Destroys all snapshots with the given name in zhp & descendants. 27052199Sahrens */ 27062199Sahrens int 27072199Sahrens zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname) 27082199Sahrens { 27092199Sahrens zfs_cmd_t zc = { 0 }; 27102199Sahrens int ret; 27112199Sahrens struct destroydata dd = { 0 }; 27122199Sahrens 27132199Sahrens dd.snapname = snapname; 27142199Sahrens (void) zfs_remove_link_cb(zhp, &dd); 27152199Sahrens 27162199Sahrens if (!dd.gotone) { 27173237Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT, 27182199Sahrens dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"), 27192199Sahrens zhp->zfs_name, snapname)); 27202199Sahrens } 27212199Sahrens 27222199Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 27232676Seschrock (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value)); 27242199Sahrens 27254543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc); 27262199Sahrens if (ret != 0) { 27272199Sahrens char errbuf[1024]; 27282199Sahrens 27292199Sahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 27302199Sahrens "cannot destroy '%s@%s'"), zc.zc_name, snapname); 27312199Sahrens 27322199Sahrens switch (errno) { 27332199Sahrens case EEXIST: 27342199Sahrens zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 27352199Sahrens "snapshot is cloned")); 27362199Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf)); 27372199Sahrens 27382199Sahrens default: 27392199Sahrens return (zfs_standard_error(zhp->zfs_hdl, errno, 27402199Sahrens errbuf)); 27412199Sahrens } 27422199Sahrens } 27432199Sahrens 27442199Sahrens return (0); 27452199Sahrens } 27462199Sahrens 2747789Sahrens /* 2748789Sahrens * Clones the given dataset. The target must be of the same type as the source. 2749789Sahrens */ 2750789Sahrens int 27512676Seschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props) 2752789Sahrens { 2753789Sahrens zfs_cmd_t zc = { 0 }; 2754789Sahrens char parent[ZFS_MAXNAMELEN]; 2755789Sahrens int ret; 27562082Seschrock char errbuf[1024]; 27572082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 27582676Seschrock zfs_type_t type; 27592676Seschrock uint64_t zoned; 2760789Sahrens 2761789Sahrens assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 2762789Sahrens 27632082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 27642082Seschrock "cannot create '%s'"), target); 27652082Seschrock 2766789Sahrens /* validate the target name */ 27672082Seschrock if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM)) 27682082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2769789Sahrens 2770789Sahrens /* validate parents exist */ 27714490Svb160487 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0) 2772789Sahrens return (-1); 2773789Sahrens 2774789Sahrens (void) parent_name(target, parent, sizeof (parent)); 2775789Sahrens 2776789Sahrens /* do the clone */ 27772676Seschrock if (ZFS_IS_VOLUME(zhp)) { 2778789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 27792744Snn35248 type = ZFS_TYPE_VOLUME; 27802676Seschrock } else { 2781789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 27822744Snn35248 type = ZFS_TYPE_FILESYSTEM; 27832676Seschrock } 27842676Seschrock 27852676Seschrock if (props) { 2786*5094Slling if ((props = zfs_validate_properties(hdl, type, props, 27873912Slling zoned, zhp, errbuf)) == NULL) 27882676Seschrock return (-1); 27892676Seschrock 2790*5094Slling if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) { 27912676Seschrock nvlist_free(props); 27922676Seschrock return (-1); 27932676Seschrock } 27942676Seschrock 27952676Seschrock nvlist_free(props); 27962676Seschrock } 2797789Sahrens 2798789Sahrens (void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name)); 27992676Seschrock (void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value)); 28004543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc); 2801789Sahrens 28022676Seschrock zcmd_free_nvlists(&zc); 28032676Seschrock 2804789Sahrens if (ret != 0) { 2805789Sahrens switch (errno) { 2806789Sahrens 2807789Sahrens case ENOENT: 2808789Sahrens /* 2809789Sahrens * The parent doesn't exist. We should have caught this 2810789Sahrens * above, but there may a race condition that has since 2811789Sahrens * destroyed the parent. 2812789Sahrens * 2813789Sahrens * At this point, we don't know whether it's the source 2814789Sahrens * that doesn't exist anymore, or whether the target 2815789Sahrens * dataset doesn't exist. 2816789Sahrens */ 28172082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 28182082Seschrock "no such parent '%s'"), parent); 28192082Seschrock return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf)); 28202082Seschrock 28212082Seschrock case EXDEV: 28222082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 28232082Seschrock "source and target pools differ")); 28242082Seschrock return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET, 28252082Seschrock errbuf)); 28262082Seschrock 28272082Seschrock default: 28282082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 28292082Seschrock errbuf)); 28302082Seschrock } 28312676Seschrock } else if (ZFS_IS_VOLUME(zhp)) { 28322082Seschrock ret = zvol_create_link(zhp->zfs_hdl, target); 28332082Seschrock } 28342082Seschrock 28352082Seschrock return (ret); 28362082Seschrock } 28372082Seschrock 28382082Seschrock typedef struct promote_data { 28392082Seschrock char cb_mountpoint[MAXPATHLEN]; 28402082Seschrock const char *cb_target; 28412082Seschrock const char *cb_errbuf; 28422082Seschrock uint64_t cb_pivot_txg; 28432082Seschrock } promote_data_t; 28442082Seschrock 28452082Seschrock static int 28462082Seschrock promote_snap_cb(zfs_handle_t *zhp, void *data) 28472082Seschrock { 28482082Seschrock promote_data_t *pd = data; 28492082Seschrock zfs_handle_t *szhp; 28502082Seschrock char snapname[MAXPATHLEN]; 28513265Sahrens int rv = 0; 28522082Seschrock 28532082Seschrock /* We don't care about snapshots after the pivot point */ 28543265Sahrens if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) { 28553265Sahrens zfs_close(zhp); 28562082Seschrock return (0); 28573265Sahrens } 28582082Seschrock 28592417Sahrens /* Remove the device link if it's a zvol. */ 28602676Seschrock if (ZFS_IS_VOLUME(zhp)) 28612417Sahrens (void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name); 28622082Seschrock 28632082Seschrock /* Check for conflicting names */ 28642676Seschrock (void) strlcpy(snapname, pd->cb_target, sizeof (snapname)); 28652676Seschrock (void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname)); 28662082Seschrock szhp = make_dataset_handle(zhp->zfs_hdl, snapname); 28672082Seschrock if (szhp != NULL) { 28682082Seschrock zfs_close(szhp); 28692082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 28702082Seschrock "snapshot name '%s' from origin \n" 28712082Seschrock "conflicts with '%s' from target"), 28722082Seschrock zhp->zfs_name, snapname); 28733265Sahrens rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf); 28742082Seschrock } 28753265Sahrens zfs_close(zhp); 28763265Sahrens return (rv); 28772082Seschrock } 28782082Seschrock 28792417Sahrens static int 28802417Sahrens promote_snap_done_cb(zfs_handle_t *zhp, void *data) 28812417Sahrens { 28822417Sahrens promote_data_t *pd = data; 28832417Sahrens 28842417Sahrens /* We don't care about snapshots after the pivot point */ 28853265Sahrens if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) { 28863265Sahrens /* Create the device link if it's a zvol. */ 28873265Sahrens if (ZFS_IS_VOLUME(zhp)) 28883265Sahrens (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 28893265Sahrens } 28903265Sahrens 28913265Sahrens zfs_close(zhp); 28922417Sahrens return (0); 28932417Sahrens } 28942417Sahrens 28952082Seschrock /* 28962082Seschrock * Promotes the given clone fs to be the clone parent. 28972082Seschrock */ 28982082Seschrock int 28992082Seschrock zfs_promote(zfs_handle_t *zhp) 29002082Seschrock { 29012082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 29022082Seschrock zfs_cmd_t zc = { 0 }; 29032082Seschrock char parent[MAXPATHLEN]; 29042082Seschrock char *cp; 29052082Seschrock int ret; 29062082Seschrock zfs_handle_t *pzhp; 29072082Seschrock promote_data_t pd; 29082082Seschrock char errbuf[1024]; 29092082Seschrock 29102082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 29112082Seschrock "cannot promote '%s'"), zhp->zfs_name); 29122082Seschrock 29132082Seschrock if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 29142082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29152082Seschrock "snapshots can not be promoted")); 29162082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 29172082Seschrock } 29182082Seschrock 29192676Seschrock (void) strlcpy(parent, zhp->zfs_dmustats.dds_clone_of, sizeof (parent)); 29202082Seschrock if (parent[0] == '\0') { 29212082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29222082Seschrock "not a cloned filesystem")); 29232082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 29242082Seschrock } 29252082Seschrock cp = strchr(parent, '@'); 29262082Seschrock *cp = '\0'; 29272082Seschrock 29282082Seschrock /* Walk the snapshots we will be moving */ 29292082Seschrock pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_clone_of, ZFS_TYPE_SNAPSHOT); 29302082Seschrock if (pzhp == NULL) 29312082Seschrock return (-1); 29322082Seschrock pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG); 29332082Seschrock zfs_close(pzhp); 29342082Seschrock pd.cb_target = zhp->zfs_name; 29352082Seschrock pd.cb_errbuf = errbuf; 2936*5094Slling pzhp = zfs_open(hdl, parent, ZFS_TYPE_DATASET); 29372082Seschrock if (pzhp == NULL) 29382082Seschrock return (-1); 29392082Seschrock (void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint, 29402082Seschrock sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE); 29412082Seschrock ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd); 29422417Sahrens if (ret != 0) { 29432417Sahrens zfs_close(pzhp); 29442082Seschrock return (-1); 29452417Sahrens } 29462082Seschrock 29472082Seschrock /* issue the ioctl */ 29482676Seschrock (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_clone_of, 29492676Seschrock sizeof (zc.zc_value)); 29502082Seschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 29514543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc); 29522082Seschrock 29532082Seschrock if (ret != 0) { 29542417Sahrens int save_errno = errno; 29552417Sahrens 29562417Sahrens (void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd); 29572417Sahrens zfs_close(pzhp); 29582417Sahrens 29592417Sahrens switch (save_errno) { 2960789Sahrens case EEXIST: 2961789Sahrens /* 29622082Seschrock * There is a conflicting snapshot name. We 29632082Seschrock * should have caught this above, but they could 29642082Seschrock * have renamed something in the mean time. 2965789Sahrens */ 29662082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29672082Seschrock "conflicting snapshot name from parent '%s'"), 29682082Seschrock parent); 29692082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 2970789Sahrens 2971789Sahrens default: 29722417Sahrens return (zfs_standard_error(hdl, save_errno, errbuf)); 2973789Sahrens } 29742417Sahrens } else { 29752417Sahrens (void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd); 2976789Sahrens } 2977789Sahrens 29782417Sahrens zfs_close(pzhp); 2979789Sahrens return (ret); 2980789Sahrens } 2981789Sahrens 29824007Smmusante struct createdata { 29834007Smmusante const char *cd_snapname; 29844007Smmusante int cd_ifexists; 29854007Smmusante }; 29864007Smmusante 29872199Sahrens static int 29882199Sahrens zfs_create_link_cb(zfs_handle_t *zhp, void *arg) 29892199Sahrens { 29904007Smmusante struct createdata *cd = arg; 29912676Seschrock int ret; 29922199Sahrens 29932199Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 29942199Sahrens char name[MAXPATHLEN]; 29952199Sahrens 29962676Seschrock (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 29972676Seschrock (void) strlcat(name, "@", sizeof (name)); 29984007Smmusante (void) strlcat(name, cd->cd_snapname, sizeof (name)); 29994007Smmusante (void) zvol_create_link_common(zhp->zfs_hdl, name, 30004007Smmusante cd->cd_ifexists); 30012199Sahrens /* 30022199Sahrens * NB: this is simply a best-effort. We don't want to 30032199Sahrens * return an error, because then we wouldn't visit all 30042199Sahrens * the volumes. 30052199Sahrens */ 30062199Sahrens } 30072676Seschrock 30084007Smmusante ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd); 30092676Seschrock 30102676Seschrock zfs_close(zhp); 30112676Seschrock 30122676Seschrock return (ret); 30132199Sahrens } 30142199Sahrens 3015789Sahrens /* 30163504Sahl * Takes a snapshot of the given dataset. 3017789Sahrens */ 3018789Sahrens int 30192199Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive) 3020789Sahrens { 3021789Sahrens const char *delim; 3022789Sahrens char *parent; 3023789Sahrens zfs_handle_t *zhp; 3024789Sahrens zfs_cmd_t zc = { 0 }; 3025789Sahrens int ret; 30262082Seschrock char errbuf[1024]; 30272082Seschrock 30282082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 30292082Seschrock "cannot snapshot '%s'"), path); 30302082Seschrock 30312082Seschrock /* validate the target name */ 30322082Seschrock if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT)) 30332082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3034789Sahrens 3035789Sahrens /* make sure the parent exists and is of the appropriate type */ 30362199Sahrens delim = strchr(path, '@'); 30372082Seschrock if ((parent = zfs_alloc(hdl, delim - path + 1)) == NULL) 30382082Seschrock return (-1); 3039789Sahrens (void) strncpy(parent, path, delim - path); 3040789Sahrens parent[delim - path] = '\0'; 3041789Sahrens 30422082Seschrock if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM | 3043789Sahrens ZFS_TYPE_VOLUME)) == NULL) { 3044789Sahrens free(parent); 3045789Sahrens return (-1); 3046789Sahrens } 3047789Sahrens 30482199Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 30492676Seschrock (void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value)); 30504543Smarks if (ZFS_IS_VOLUME(zhp)) 30514543Smarks zc.zc_objset_type = DMU_OST_ZVOL; 30524543Smarks else 30534543Smarks zc.zc_objset_type = DMU_OST_ZFS; 30542199Sahrens zc.zc_cookie = recursive; 30554543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc); 30562199Sahrens 30572199Sahrens /* 30582199Sahrens * if it was recursive, the one that actually failed will be in 30592199Sahrens * zc.zc_name. 30602199Sahrens */ 30614543Smarks if (ret != 0) 30624543Smarks (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 30634543Smarks "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value); 30644543Smarks 30652199Sahrens if (ret == 0 && recursive) { 30664007Smmusante struct createdata cd; 30674007Smmusante 30684007Smmusante cd.cd_snapname = delim + 1; 30694007Smmusante cd.cd_ifexists = B_FALSE; 30704007Smmusante (void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd); 30712199Sahrens } 3072789Sahrens if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) { 30732082Seschrock ret = zvol_create_link(zhp->zfs_hdl, path); 30742199Sahrens if (ret != 0) { 30754543Smarks (void) zfs_standard_error(hdl, errno, 30764543Smarks dgettext(TEXT_DOMAIN, 30774543Smarks "Volume successfully snapshotted, but device links " 30784543Smarks "were not created")); 30794543Smarks free(parent); 30804543Smarks zfs_close(zhp); 30814543Smarks return (-1); 30822199Sahrens } 3083789Sahrens } 3084789Sahrens 30852082Seschrock if (ret != 0) 30862082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 3087789Sahrens 3088789Sahrens free(parent); 3089789Sahrens zfs_close(zhp); 3090789Sahrens 3091789Sahrens return (ret); 3092789Sahrens } 3093789Sahrens 3094789Sahrens /* 30953504Sahl * Dumps a backup of the given snapshot (incremental from fromsnap if it's not 30963504Sahl * NULL) to the file descriptor specified by outfd. 3097789Sahrens */ 3098789Sahrens int 30993504Sahl zfs_send(zfs_handle_t *zhp, const char *fromsnap, int outfd) 3100789Sahrens { 3101789Sahrens zfs_cmd_t zc = { 0 }; 31022082Seschrock char errbuf[1024]; 31032885Sahrens libzfs_handle_t *hdl = zhp->zfs_hdl; 31042082Seschrock 31053504Sahl assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 31063504Sahl 31072885Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 31082885Sahrens if (fromsnap) 31092885Sahrens (void) strlcpy(zc.zc_value, fromsnap, sizeof (zc.zc_name)); 31103504Sahl zc.zc_cookie = outfd; 31113504Sahl 31123504Sahl if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SENDBACKUP, &zc) != 0) { 31133504Sahl (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 31143504Sahl "cannot send '%s'"), zhp->zfs_name); 31153504Sahl 3116789Sahrens switch (errno) { 3117789Sahrens 3118789Sahrens case EXDEV: 31192082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 31203413Smmusante "not an earlier snapshot from the same fs")); 31212082Seschrock return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 3122789Sahrens 3123789Sahrens case EDQUOT: 3124789Sahrens case EFBIG: 3125789Sahrens case EIO: 3126789Sahrens case ENOLINK: 3127789Sahrens case ENOSPC: 3128789Sahrens case ENOSTR: 3129789Sahrens case ENXIO: 3130789Sahrens case EPIPE: 3131789Sahrens case ERANGE: 3132789Sahrens case EFAULT: 3133789Sahrens case EROFS: 31342082Seschrock zfs_error_aux(hdl, strerror(errno)); 31352082Seschrock return (zfs_error(hdl, EZFS_BADBACKUP, errbuf)); 3136789Sahrens 3137789Sahrens default: 31382082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 3139789Sahrens } 3140789Sahrens } 3141789Sahrens 31423504Sahl return (0); 3143789Sahrens } 3144789Sahrens 3145789Sahrens /* 31462885Sahrens * Create ancestors of 'target', but not target itself, and not 31472885Sahrens * ancestors whose names are shorter than prefixlen. Die if 31482885Sahrens * prefixlen-ancestor does not exist. 31492885Sahrens */ 31502885Sahrens static int 31512885Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen) 31522885Sahrens { 31532885Sahrens zfs_handle_t *h; 31542885Sahrens char *cp; 31552885Sahrens 31562885Sahrens /* make sure prefix exists */ 31572885Sahrens cp = strchr(target + prefixlen, '/'); 31584603Sahrens if (cp == NULL) { 31594603Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 31604603Sahrens } else { 31614603Sahrens *cp = '\0'; 31624603Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 31634603Sahrens *cp = '/'; 31644603Sahrens } 31652885Sahrens if (h == NULL) 31662885Sahrens return (-1); 31672885Sahrens zfs_close(h); 31682885Sahrens 31692885Sahrens /* 31702885Sahrens * Attempt to create, mount, and share any ancestor filesystems, 31712885Sahrens * up to the prefixlen-long one. 31722885Sahrens */ 31732885Sahrens for (cp = target + prefixlen + 1; 31742885Sahrens cp = strchr(cp, '/'); *cp = '/', cp++) { 31752885Sahrens const char *opname; 31764543Smarks char *logstr; 31772885Sahrens 31782885Sahrens *cp = '\0'; 31792885Sahrens 31802885Sahrens h = make_dataset_handle(hdl, target); 31812885Sahrens if (h) { 31822885Sahrens /* it already exists, nothing to do here */ 31832885Sahrens zfs_close(h); 31842885Sahrens continue; 31852885Sahrens } 31862885Sahrens 31872885Sahrens opname = dgettext(TEXT_DOMAIN, "create"); 31884543Smarks logstr = hdl->libzfs_log_str; 31894543Smarks hdl->libzfs_log_str = NULL; 31902885Sahrens if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM, 31914543Smarks NULL) != 0) { 31924543Smarks hdl->libzfs_log_str = logstr; 31932885Sahrens goto ancestorerr; 31944543Smarks } 31954543Smarks 31964543Smarks hdl->libzfs_log_str = logstr; 31972885Sahrens opname = dgettext(TEXT_DOMAIN, "open"); 31982885Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 31992885Sahrens if (h == NULL) 32002885Sahrens goto ancestorerr; 32012885Sahrens 32022885Sahrens opname = dgettext(TEXT_DOMAIN, "mount"); 32032885Sahrens if (zfs_mount(h, NULL, 0) != 0) 32042885Sahrens goto ancestorerr; 32052885Sahrens 32062885Sahrens opname = dgettext(TEXT_DOMAIN, "share"); 32072885Sahrens if (zfs_share(h) != 0) 32082885Sahrens goto ancestorerr; 32092885Sahrens 32102885Sahrens zfs_close(h); 32112885Sahrens 32122885Sahrens continue; 32132885Sahrens ancestorerr: 32142885Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 32152885Sahrens "failed to %s ancestor '%s'"), opname, target); 32162885Sahrens return (-1); 32172885Sahrens } 32182885Sahrens 32192885Sahrens return (0); 32202885Sahrens } 32212885Sahrens 32222885Sahrens /* 32233504Sahl * Restores a backup of tosnap from the file descriptor specified by infd. 3224789Sahrens */ 3225789Sahrens int 32262082Seschrock zfs_receive(libzfs_handle_t *hdl, const char *tosnap, int isprefix, 32273504Sahl int verbose, int dryrun, boolean_t force, int infd) 3228789Sahrens { 3229789Sahrens zfs_cmd_t zc = { 0 }; 3230789Sahrens time_t begin_time; 32312885Sahrens int ioctl_err, err, bytes, size, choplen; 3232789Sahrens char *cp; 3233789Sahrens dmu_replay_record_t drr; 3234789Sahrens struct drr_begin *drrb = &zc.zc_begin_record; 32352082Seschrock char errbuf[1024]; 32362665Snd150628 prop_changelist_t *clp; 32372885Sahrens char chopprefix[ZFS_MAXNAMELEN]; 3238789Sahrens 3239789Sahrens begin_time = time(NULL); 3240789Sahrens 32412082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 32422082Seschrock "cannot receive")); 32432082Seschrock 3244789Sahrens /* read in the BEGIN record */ 3245789Sahrens cp = (char *)&drr; 3246789Sahrens bytes = 0; 3247789Sahrens do { 32483504Sahl size = read(infd, cp, sizeof (drr) - bytes); 3249868Sahrens cp += size; 3250868Sahrens bytes += size; 3251868Sahrens } while (size > 0); 3252868Sahrens 3253868Sahrens if (size < 0 || bytes != sizeof (drr)) { 32542082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 32552082Seschrock "stream (failed to read first record)")); 32562082Seschrock return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 3257789Sahrens } 3258789Sahrens 3259789Sahrens zc.zc_begin_record = drr.drr_u.drr_begin; 3260789Sahrens 3261789Sahrens if (drrb->drr_magic != DMU_BACKUP_MAGIC && 3262789Sahrens drrb->drr_magic != BSWAP_64(DMU_BACKUP_MAGIC)) { 32632082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 32642082Seschrock "stream (bad magic number)")); 32652082Seschrock return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 3266789Sahrens } 3267789Sahrens 3268789Sahrens if (drrb->drr_version != DMU_BACKUP_VERSION && 3269789Sahrens drrb->drr_version != BSWAP_64(DMU_BACKUP_VERSION)) { 32702082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only version " 32712082Seschrock "0x%llx is supported (stream is version 0x%llx)"), 3272789Sahrens DMU_BACKUP_VERSION, drrb->drr_version); 32732082Seschrock return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 3274789Sahrens } 3275789Sahrens 32762885Sahrens if (strchr(drr.drr_u.drr_begin.drr_toname, '@') == NULL) { 32772885Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 32783912Slling "stream (bad snapshot name)")); 32792885Sahrens return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 32802885Sahrens } 3281789Sahrens /* 32822885Sahrens * Determine how much of the snapshot name stored in the stream 32832885Sahrens * we are going to tack on to the name they specified on the 32842885Sahrens * command line, and how much we are going to chop off. 32852885Sahrens * 32862885Sahrens * If they specified a snapshot, chop the entire name stored in 32872885Sahrens * the stream. 3288789Sahrens */ 32892885Sahrens (void) strcpy(chopprefix, drr.drr_u.drr_begin.drr_toname); 3290789Sahrens if (isprefix) { 32912885Sahrens /* 32922885Sahrens * They specified a fs with -d, we want to tack on 32932885Sahrens * everything but the pool name stored in the stream 32942885Sahrens */ 32952885Sahrens if (strchr(tosnap, '@')) { 32962885Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 32972885Sahrens "argument - snapshot not allowed with -d")); 32982885Sahrens return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3299789Sahrens } 33002885Sahrens cp = strchr(chopprefix, '/'); 3301789Sahrens if (cp == NULL) 33022885Sahrens cp = strchr(chopprefix, '@'); 33032885Sahrens *cp = '\0'; 3304789Sahrens } else if (strchr(tosnap, '@') == NULL) { 3305789Sahrens /* 33062885Sahrens * If they specified a filesystem without -d, we want to 33072885Sahrens * tack on everything after the fs specified in the 33082885Sahrens * first name from the stream. 3309789Sahrens */ 33102885Sahrens cp = strchr(chopprefix, '@'); 33112885Sahrens *cp = '\0'; 3312789Sahrens } 33132885Sahrens choplen = strlen(chopprefix); 33142885Sahrens 33152885Sahrens /* 33162885Sahrens * Determine name of destination snapshot, store in zc_value. 33172885Sahrens */ 33182885Sahrens (void) strcpy(zc.zc_value, tosnap); 33192885Sahrens (void) strncat(zc.zc_value, drr.drr_u.drr_begin.drr_toname+choplen, 33202885Sahrens sizeof (zc.zc_value)); 33213265Sahrens if (!zfs_validate_name(hdl, zc.zc_value, ZFS_TYPE_SNAPSHOT)) 33223265Sahrens return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 33232885Sahrens 33242885Sahrens (void) strcpy(zc.zc_name, zc.zc_value); 3325789Sahrens if (drrb->drr_fromguid) { 3326789Sahrens /* incremental backup stream */ 33272885Sahrens zfs_handle_t *h; 33282885Sahrens 33292885Sahrens /* do the recvbackup ioctl to the containing fs */ 33302885Sahrens *strchr(zc.zc_name, '@') = '\0'; 3331789Sahrens 3332789Sahrens /* make sure destination fs exists */ 33332082Seschrock h = zfs_open(hdl, zc.zc_name, 33342082Seschrock ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 33352082Seschrock if (h == NULL) 3336789Sahrens return (-1); 3337868Sahrens if (!dryrun) { 33382665Snd150628 /* 33392665Snd150628 * We need to unmount all the dependents of the dataset 33402665Snd150628 * and the dataset itself. If it's a volume 33412665Snd150628 * then remove device link. 33422665Snd150628 */ 3343868Sahrens if (h->zfs_type == ZFS_TYPE_FILESYSTEM) { 33442665Snd150628 clp = changelist_gather(h, ZFS_PROP_NAME, 0); 33452665Snd150628 if (clp == NULL) 33462665Snd150628 return (-1); 33472665Snd150628 if (changelist_prefix(clp) != 0) { 33482665Snd150628 changelist_free(clp); 33492665Snd150628 return (-1); 33502665Snd150628 } 3351868Sahrens } else { 33524543Smarks if (zvol_remove_link(hdl, h->zfs_name) != 0) { 33534543Smarks zfs_close(h); 33544543Smarks return (-1); 33554543Smarks } 33564543Smarks 3357868Sahrens } 3358868Sahrens } 3359789Sahrens zfs_close(h); 3360789Sahrens } else { 3361789Sahrens /* full backup stream */ 3362789Sahrens 3363868Sahrens /* Make sure destination fs does not exist */ 33642885Sahrens *strchr(zc.zc_name, '@') = '\0'; 3365*5094Slling if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) { 33662082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 33672082Seschrock "destination '%s' exists"), zc.zc_name); 33682082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3369868Sahrens } 3370868Sahrens 33712885Sahrens if (strchr(zc.zc_name, '/') == NULL) { 33722885Sahrens /* 33732885Sahrens * they're trying to do a recv into a 33742885Sahrens * nonexistant topmost filesystem. 33752885Sahrens */ 33762885Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 33772885Sahrens "destination does not exist"), zc.zc_name); 33782885Sahrens return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 33792885Sahrens } 33802885Sahrens 3381868Sahrens /* Do the recvbackup ioctl to the fs's parent. */ 33822885Sahrens *strrchr(zc.zc_name, '/') = '\0'; 33832885Sahrens 33842885Sahrens if (isprefix && (err = create_parents(hdl, 33852885Sahrens zc.zc_value, strlen(tosnap))) != 0) { 33862885Sahrens return (zfs_error(hdl, EZFS_BADRESTORE, errbuf)); 33872885Sahrens } 33882885Sahrens 3389789Sahrens } 3390789Sahrens 33913504Sahl zc.zc_cookie = infd; 33922676Seschrock zc.zc_guid = force; 3393789Sahrens if (verbose) { 33941749Sahrens (void) printf("%s %s stream of %s into %s\n", 33951749Sahrens dryrun ? "would receive" : "receiving", 3396789Sahrens drrb->drr_fromguid ? "incremental" : "full", 3397789Sahrens drr.drr_u.drr_begin.drr_toname, 33982676Seschrock zc.zc_value); 3399789Sahrens (void) fflush(stdout); 3400789Sahrens } 3401789Sahrens if (dryrun) 3402789Sahrens return (0); 34034543Smarks err = ioctl_err = zfs_ioctl(hdl, ZFS_IOC_RECVBACKUP, &zc); 3404868Sahrens if (ioctl_err != 0) { 3405789Sahrens switch (errno) { 3406789Sahrens case ENODEV: 34072082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 34082082Seschrock "most recent snapshot does not match incremental " 34092082Seschrock "source")); 34102082Seschrock (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf); 3411789Sahrens break; 3412789Sahrens case ETXTBSY: 34132082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 34142082Seschrock "destination has been modified since most recent " 34152082Seschrock "snapshot")); 34162082Seschrock (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf); 3417789Sahrens break; 3418789Sahrens case EEXIST: 3419789Sahrens if (drrb->drr_fromguid == 0) { 3420789Sahrens /* it's the containing fs that exists */ 34212676Seschrock cp = strchr(zc.zc_value, '@'); 3422789Sahrens *cp = '\0'; 3423789Sahrens } 34242082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 34252082Seschrock "destination already exists")); 34263237Slling (void) zfs_error_fmt(hdl, EZFS_EXISTS, 34273237Slling dgettext(TEXT_DOMAIN, "cannot restore to %s"), 34283237Slling zc.zc_value); 3429789Sahrens break; 3430789Sahrens case EINVAL: 34312082Seschrock (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf); 3432868Sahrens break; 34331544Seschrock case ECKSUM: 34342082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 34352082Seschrock "invalid stream (checksum mismatch)")); 34362082Seschrock (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf); 3437789Sahrens break; 3438789Sahrens default: 34392082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 3440789Sahrens } 3441789Sahrens } 3442789Sahrens 3443789Sahrens /* 3444868Sahrens * Mount or recreate the /dev links for the target filesystem 3445868Sahrens * (if created, or if we tore them down to do an incremental 3446868Sahrens * restore), and the /dev links for the new snapshot (if 34472665Snd150628 * created). Also mount any children of the target filesystem 34482665Snd150628 * if we did an incremental receive. 3449789Sahrens */ 34502676Seschrock cp = strchr(zc.zc_value, '@'); 3451868Sahrens if (cp && (ioctl_err == 0 || drrb->drr_fromguid)) { 3452789Sahrens zfs_handle_t *h; 3453789Sahrens 3454789Sahrens *cp = '\0'; 34552676Seschrock h = zfs_open(hdl, zc.zc_value, 3456789Sahrens ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 3457868Sahrens *cp = '@'; 3458789Sahrens if (h) { 34592665Snd150628 if (h->zfs_type == ZFS_TYPE_VOLUME) { 34602082Seschrock err = zvol_create_link(hdl, h->zfs_name); 34611544Seschrock if (err == 0 && ioctl_err == 0) 34622082Seschrock err = zvol_create_link(hdl, 34632676Seschrock zc.zc_value); 34642665Snd150628 } else { 34652665Snd150628 if (drrb->drr_fromguid) { 34662665Snd150628 err = changelist_postfix(clp); 34672665Snd150628 changelist_free(clp); 34682665Snd150628 } else { 34692665Snd150628 err = zfs_mount(h, NULL, 0); 34702665Snd150628 } 3471868Sahrens } 34722665Snd150628 zfs_close(h); 3473789Sahrens } 3474789Sahrens } 3475789Sahrens 3476868Sahrens if (err || ioctl_err) 3477868Sahrens return (-1); 3478789Sahrens 3479789Sahrens if (verbose) { 3480789Sahrens char buf1[64]; 3481789Sahrens char buf2[64]; 3482789Sahrens uint64_t bytes = zc.zc_cookie; 3483789Sahrens time_t delta = time(NULL) - begin_time; 3484789Sahrens if (delta == 0) 3485789Sahrens delta = 1; 3486789Sahrens zfs_nicenum(bytes, buf1, sizeof (buf1)); 3487789Sahrens zfs_nicenum(bytes/delta, buf2, sizeof (buf1)); 3488789Sahrens 34894603Sahrens (void) printf("received %sB stream in %lu seconds (%sB/sec)\n", 3490789Sahrens buf1, delta, buf2); 3491789Sahrens } 34922665Snd150628 3493789Sahrens return (0); 3494789Sahrens } 3495789Sahrens 3496789Sahrens /* 34971294Slling * Destroy any more recent snapshots. We invoke this callback on any dependents 34981294Slling * of the snapshot first. If the 'cb_dependent' member is non-zero, then this 34991294Slling * is a dependent and we should just destroy it without checking the transaction 35001294Slling * group. 3501789Sahrens */ 35021294Slling typedef struct rollback_data { 35031294Slling const char *cb_target; /* the snapshot */ 35041294Slling uint64_t cb_create; /* creation time reference */ 35051294Slling prop_changelist_t *cb_clp; /* changelist pointer */ 35061294Slling int cb_error; 35072082Seschrock boolean_t cb_dependent; 35081294Slling } rollback_data_t; 35091294Slling 35101294Slling static int 35111294Slling rollback_destroy(zfs_handle_t *zhp, void *data) 35121294Slling { 35131294Slling rollback_data_t *cbp = data; 35141294Slling 35151294Slling if (!cbp->cb_dependent) { 35161294Slling if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 && 35171294Slling zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT && 35181294Slling zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > 35191294Slling cbp->cb_create) { 35204543Smarks char *logstr; 35211294Slling 35222082Seschrock cbp->cb_dependent = B_TRUE; 35232474Seschrock if (zfs_iter_dependents(zhp, B_FALSE, rollback_destroy, 35242474Seschrock cbp) != 0) 35252474Seschrock cbp->cb_error = 1; 35262082Seschrock cbp->cb_dependent = B_FALSE; 35271294Slling 35284543Smarks logstr = zhp->zfs_hdl->libzfs_log_str; 35294543Smarks zhp->zfs_hdl->libzfs_log_str = NULL; 35301294Slling if (zfs_destroy(zhp) != 0) 35311294Slling cbp->cb_error = 1; 35321294Slling else 35331294Slling changelist_remove(zhp, cbp->cb_clp); 35344543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 35351294Slling } 35361294Slling } else { 35371294Slling if (zfs_destroy(zhp) != 0) 35381294Slling cbp->cb_error = 1; 35391294Slling else 35401294Slling changelist_remove(zhp, cbp->cb_clp); 35411294Slling } 35421294Slling 35431294Slling zfs_close(zhp); 35441294Slling return (0); 35451294Slling } 35461294Slling 35471294Slling /* 35481294Slling * Rollback the dataset to its latest snapshot. 35491294Slling */ 35501294Slling static int 35511294Slling do_rollback(zfs_handle_t *zhp) 3552789Sahrens { 3553789Sahrens int ret; 3554789Sahrens zfs_cmd_t zc = { 0 }; 3555789Sahrens 3556789Sahrens assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM || 3557789Sahrens zhp->zfs_type == ZFS_TYPE_VOLUME); 3558789Sahrens 3559789Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME && 35602082Seschrock zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) 3561789Sahrens return (-1); 3562789Sahrens 3563789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3564789Sahrens 35652676Seschrock if (ZFS_IS_VOLUME(zhp)) 3566789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3567789Sahrens else 3568789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3569789Sahrens 3570789Sahrens /* 3571789Sahrens * We rely on the consumer to verify that there are no newer snapshots 3572789Sahrens * for the given dataset. Given these constraints, we can simply pass 3573789Sahrens * the name on to the ioctl() call. There is still an unlikely race 3574789Sahrens * condition where the user has taken a snapshot since we verified that 3575789Sahrens * this was the most recent. 3576789Sahrens */ 35774543Smarks if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) { 35783237Slling (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno, 35792082Seschrock dgettext(TEXT_DOMAIN, "cannot rollback '%s'"), 35802082Seschrock zhp->zfs_name); 3581789Sahrens } else if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 35822082Seschrock ret = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 3583789Sahrens } 3584789Sahrens 3585789Sahrens return (ret); 3586789Sahrens } 3587789Sahrens 3588789Sahrens /* 35891294Slling * Given a dataset, rollback to a specific snapshot, discarding any 35901294Slling * data changes since then and making it the active dataset. 35911294Slling * 35921294Slling * Any snapshots more recent than the target are destroyed, along with 35931294Slling * their dependents. 35941294Slling */ 35951294Slling int 35961294Slling zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, int flag) 35971294Slling { 35981294Slling int ret; 35991294Slling rollback_data_t cb = { 0 }; 36001294Slling prop_changelist_t *clp; 36011294Slling 36021294Slling /* 36031294Slling * Unmount all dependendents of the dataset and the dataset itself. 36041294Slling * The list we need to gather is the same as for doing rename 36051294Slling */ 36061294Slling clp = changelist_gather(zhp, ZFS_PROP_NAME, flag ? MS_FORCE: 0); 36071294Slling if (clp == NULL) 36081294Slling return (-1); 36091294Slling 36101294Slling if ((ret = changelist_prefix(clp)) != 0) 36111294Slling goto out; 36121294Slling 36131294Slling /* 36141294Slling * Destroy all recent snapshots and its dependends. 36151294Slling */ 36161294Slling cb.cb_target = snap->zfs_name; 36171294Slling cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG); 36181294Slling cb.cb_clp = clp; 36191294Slling (void) zfs_iter_children(zhp, rollback_destroy, &cb); 36201294Slling 36211294Slling if ((ret = cb.cb_error) != 0) { 36221294Slling (void) changelist_postfix(clp); 36231294Slling goto out; 36241294Slling } 36251294Slling 36261294Slling /* 36271294Slling * Now that we have verified that the snapshot is the latest, 36281294Slling * rollback to the given snapshot. 36291294Slling */ 36301294Slling ret = do_rollback(zhp); 36311294Slling 36321294Slling if (ret != 0) { 36331294Slling (void) changelist_postfix(clp); 36341294Slling goto out; 36351294Slling } 36361294Slling 36371294Slling /* 36381294Slling * We only want to re-mount the filesystem if it was mounted in the 36391294Slling * first place. 36401294Slling */ 36411294Slling ret = changelist_postfix(clp); 36421294Slling 36431294Slling out: 36441294Slling changelist_free(clp); 36451294Slling return (ret); 36461294Slling } 36471294Slling 36481294Slling /* 3649789Sahrens * Iterate over all dependents for a given dataset. This includes both 3650789Sahrens * hierarchical dependents (children) and data dependents (snapshots and 3651789Sahrens * clones). The bulk of the processing occurs in get_dependents() in 3652789Sahrens * libzfs_graph.c. 3653789Sahrens */ 3654789Sahrens int 36552474Seschrock zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion, 36562474Seschrock zfs_iter_f func, void *data) 3657789Sahrens { 3658789Sahrens char **dependents; 3659789Sahrens size_t count; 3660789Sahrens int i; 3661789Sahrens zfs_handle_t *child; 3662789Sahrens int ret = 0; 3663789Sahrens 36642474Seschrock if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name, 36652474Seschrock &dependents, &count) != 0) 36662474Seschrock return (-1); 36672474Seschrock 3668789Sahrens for (i = 0; i < count; i++) { 36692082Seschrock if ((child = make_dataset_handle(zhp->zfs_hdl, 36702082Seschrock dependents[i])) == NULL) 3671789Sahrens continue; 3672789Sahrens 3673789Sahrens if ((ret = func(child, data)) != 0) 3674789Sahrens break; 3675789Sahrens } 3676789Sahrens 3677789Sahrens for (i = 0; i < count; i++) 3678789Sahrens free(dependents[i]); 3679789Sahrens free(dependents); 3680789Sahrens 3681789Sahrens return (ret); 3682789Sahrens } 3683789Sahrens 3684789Sahrens /* 3685789Sahrens * Renames the given dataset. 3686789Sahrens */ 3687789Sahrens int 36884490Svb160487 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive) 3689789Sahrens { 3690789Sahrens int ret; 3691789Sahrens zfs_cmd_t zc = { 0 }; 3692789Sahrens char *delim; 36934007Smmusante prop_changelist_t *cl = NULL; 36944007Smmusante zfs_handle_t *zhrp = NULL; 36954007Smmusante char *parentname = NULL; 3696789Sahrens char parent[ZFS_MAXNAMELEN]; 36972082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 36982082Seschrock char errbuf[1024]; 3699789Sahrens 3700789Sahrens /* if we have the same exact name, just return success */ 3701789Sahrens if (strcmp(zhp->zfs_name, target) == 0) 3702789Sahrens return (0); 3703789Sahrens 37042082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 37052082Seschrock "cannot rename to '%s'"), target); 37062082Seschrock 3707789Sahrens /* 3708789Sahrens * Make sure the target name is valid 3709789Sahrens */ 3710789Sahrens if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 37112665Snd150628 if ((strchr(target, '@') == NULL) || 37122665Snd150628 *target == '@') { 37132665Snd150628 /* 37142665Snd150628 * Snapshot target name is abbreviated, 37152665Snd150628 * reconstruct full dataset name 37162665Snd150628 */ 37172665Snd150628 (void) strlcpy(parent, zhp->zfs_name, 37182665Snd150628 sizeof (parent)); 37192665Snd150628 delim = strchr(parent, '@'); 37202665Snd150628 if (strchr(target, '@') == NULL) 37212665Snd150628 *(++delim) = '\0'; 37222665Snd150628 else 37232665Snd150628 *delim = '\0'; 37242665Snd150628 (void) strlcat(parent, target, sizeof (parent)); 37252665Snd150628 target = parent; 37262665Snd150628 } else { 37272665Snd150628 /* 37282665Snd150628 * Make sure we're renaming within the same dataset. 37292665Snd150628 */ 37302665Snd150628 delim = strchr(target, '@'); 37312665Snd150628 if (strncmp(zhp->zfs_name, target, delim - target) 37322665Snd150628 != 0 || zhp->zfs_name[delim - target] != '@') { 37332665Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37342665Snd150628 "snapshots must be part of same " 37352665Snd150628 "dataset")); 37362665Snd150628 return (zfs_error(hdl, EZFS_CROSSTARGET, 37373912Slling errbuf)); 37382665Snd150628 } 3739789Sahrens } 37402665Snd150628 if (!zfs_validate_name(hdl, target, zhp->zfs_type)) 37412665Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3742789Sahrens } else { 37434007Smmusante if (recursive) { 37444007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37454007Smmusante "recursive rename must be a snapshot")); 37464007Smmusante return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 37474007Smmusante } 37484007Smmusante 37492665Snd150628 if (!zfs_validate_name(hdl, target, zhp->zfs_type)) 37502665Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 37512676Seschrock uint64_t unused; 37522676Seschrock 3753789Sahrens /* validate parents */ 37544490Svb160487 if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0) 3755789Sahrens return (-1); 3756789Sahrens 3757789Sahrens (void) parent_name(target, parent, sizeof (parent)); 3758789Sahrens 3759789Sahrens /* make sure we're in the same pool */ 3760789Sahrens verify((delim = strchr(target, '/')) != NULL); 3761789Sahrens if (strncmp(zhp->zfs_name, target, delim - target) != 0 || 3762789Sahrens zhp->zfs_name[delim - target] != '/') { 37632082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37642082Seschrock "datasets must be within same pool")); 37652082Seschrock return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 3766789Sahrens } 37672440Snd150628 37682440Snd150628 /* new name cannot be a child of the current dataset name */ 37692440Snd150628 if (strncmp(parent, zhp->zfs_name, 37703912Slling strlen(zhp->zfs_name)) == 0) { 37712440Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37722440Snd150628 "New dataset name cannot be a descendent of " 37732440Snd150628 "current dataset name")); 37742440Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 37752440Snd150628 } 3776789Sahrens } 3777789Sahrens 37782082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 37792082Seschrock dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name); 37802082Seschrock 3781789Sahrens if (getzoneid() == GLOBAL_ZONEID && 3782789Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 37832082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37842082Seschrock "dataset is used in a non-global zone")); 37852082Seschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 3786789Sahrens } 3787789Sahrens 37884007Smmusante if (recursive) { 37894007Smmusante struct destroydata dd; 37904007Smmusante 37914183Smmusante parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name); 37924183Smmusante if (parentname == NULL) { 37934183Smmusante ret = -1; 37944183Smmusante goto error; 37954183Smmusante } 37964007Smmusante delim = strchr(parentname, '@'); 37974007Smmusante *delim = '\0'; 3798*5094Slling zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET); 37994007Smmusante if (zhrp == NULL) { 38004183Smmusante ret = -1; 38014183Smmusante goto error; 38024007Smmusante } 38034007Smmusante 38044007Smmusante dd.snapname = delim + 1; 38054007Smmusante dd.gotone = B_FALSE; 38064183Smmusante dd.closezhp = B_TRUE; 38074007Smmusante 38084007Smmusante /* We remove any zvol links prior to renaming them */ 38094007Smmusante ret = zfs_iter_filesystems(zhrp, zfs_remove_link_cb, &dd); 38104007Smmusante if (ret) { 38114007Smmusante goto error; 38124007Smmusante } 38134007Smmusante } else { 38144007Smmusante if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0)) == NULL) 38154007Smmusante return (-1); 38164007Smmusante 38174007Smmusante if (changelist_haszonedchild(cl)) { 38184007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 38194007Smmusante "child dataset with inherited mountpoint is used " 38204007Smmusante "in a non-global zone")); 38214007Smmusante (void) zfs_error(hdl, EZFS_ZONED, errbuf); 38224007Smmusante goto error; 38234007Smmusante } 38244007Smmusante 38254007Smmusante if ((ret = changelist_prefix(cl)) != 0) 38264007Smmusante goto error; 3827789Sahrens } 3828789Sahrens 38292676Seschrock if (ZFS_IS_VOLUME(zhp)) 3830789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3831789Sahrens else 3832789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3833789Sahrens 38342665Snd150628 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 38352676Seschrock (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value)); 38362665Snd150628 38374007Smmusante zc.zc_cookie = recursive; 38384007Smmusante 38394543Smarks if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) { 38404007Smmusante /* 38414007Smmusante * if it was recursive, the one that actually failed will 38424007Smmusante * be in zc.zc_name 38434007Smmusante */ 38444007Smmusante (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 38454007Smmusante "cannot rename to '%s'"), zc.zc_name); 38464007Smmusante 38474007Smmusante if (recursive && errno == EEXIST) { 38484007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 38494007Smmusante "a child dataset already has a snapshot " 38504007Smmusante "with the new name")); 38514801Seschrock (void) zfs_error(hdl, EZFS_EXISTS, errbuf); 38524007Smmusante } else { 38534007Smmusante (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf); 38544007Smmusante } 3855789Sahrens 3856789Sahrens /* 3857789Sahrens * On failure, we still want to remount any filesystems that 3858789Sahrens * were previously mounted, so we don't alter the system state. 3859789Sahrens */ 38604007Smmusante if (recursive) { 38614007Smmusante struct createdata cd; 38624007Smmusante 38634007Smmusante /* only create links for datasets that had existed */ 38644007Smmusante cd.cd_snapname = delim + 1; 38654007Smmusante cd.cd_ifexists = B_TRUE; 38664007Smmusante (void) zfs_iter_filesystems(zhrp, zfs_create_link_cb, 38674007Smmusante &cd); 38684007Smmusante } else { 38694007Smmusante (void) changelist_postfix(cl); 38704007Smmusante } 3871789Sahrens } else { 38724007Smmusante if (recursive) { 38734007Smmusante struct createdata cd; 38744007Smmusante 38754007Smmusante /* only create links for datasets that had existed */ 38764007Smmusante cd.cd_snapname = strchr(target, '@') + 1; 38774007Smmusante cd.cd_ifexists = B_TRUE; 38784007Smmusante ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb, 38794007Smmusante &cd); 38804007Smmusante } else { 38814007Smmusante changelist_rename(cl, zfs_get_name(zhp), target); 38824007Smmusante ret = changelist_postfix(cl); 38834007Smmusante } 3884789Sahrens } 3885789Sahrens 3886789Sahrens error: 38874007Smmusante if (parentname) { 38884007Smmusante free(parentname); 38894007Smmusante } 38904007Smmusante if (zhrp) { 38914007Smmusante zfs_close(zhrp); 38924007Smmusante } 38934007Smmusante if (cl) { 38944007Smmusante changelist_free(cl); 38954007Smmusante } 3896789Sahrens return (ret); 3897789Sahrens } 3898789Sahrens 3899789Sahrens /* 3900789Sahrens * Given a zvol dataset, issue the ioctl to create the appropriate minor node, 3901789Sahrens * poke devfsadm to create the /dev link, and then wait for the link to appear. 3902789Sahrens */ 3903789Sahrens int 39042082Seschrock zvol_create_link(libzfs_handle_t *hdl, const char *dataset) 3905789Sahrens { 39064007Smmusante return (zvol_create_link_common(hdl, dataset, B_FALSE)); 39074007Smmusante } 39084007Smmusante 39094007Smmusante static int 39104007Smmusante zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists) 39114007Smmusante { 3912789Sahrens zfs_cmd_t zc = { 0 }; 39132082Seschrock di_devlink_handle_t dhdl; 39144543Smarks priv_set_t *priv_effective; 39154543Smarks int privileged; 3916789Sahrens 3917789Sahrens (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 3918789Sahrens 3919789Sahrens /* 3920789Sahrens * Issue the appropriate ioctl. 3921789Sahrens */ 39222082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) { 3923789Sahrens switch (errno) { 3924789Sahrens case EEXIST: 3925789Sahrens /* 3926789Sahrens * Silently ignore the case where the link already 3927789Sahrens * exists. This allows 'zfs volinit' to be run multiple 3928789Sahrens * times without errors. 3929789Sahrens */ 3930789Sahrens return (0); 3931789Sahrens 39324007Smmusante case ENOENT: 39334007Smmusante /* 39344007Smmusante * Dataset does not exist in the kernel. If we 39354007Smmusante * don't care (see zfs_rename), then ignore the 39364007Smmusante * error quietly. 39374007Smmusante */ 39384007Smmusante if (ifexists) { 39394007Smmusante return (0); 39404007Smmusante } 39414007Smmusante 39424007Smmusante /* FALLTHROUGH */ 39434007Smmusante 3944789Sahrens default: 39453237Slling return (zfs_standard_error_fmt(hdl, errno, 39462082Seschrock dgettext(TEXT_DOMAIN, "cannot create device links " 39472082Seschrock "for '%s'"), dataset)); 3948789Sahrens } 3949789Sahrens } 3950789Sahrens 3951789Sahrens /* 39524543Smarks * If privileged call devfsadm and wait for the links to 39534543Smarks * magically appear. 39544543Smarks * Otherwise, print out an informational message. 3955789Sahrens */ 39564543Smarks 39574543Smarks priv_effective = priv_allocset(); 39584543Smarks (void) getppriv(PRIV_EFFECTIVE, priv_effective); 39594543Smarks privileged = (priv_isfullset(priv_effective) == B_TRUE); 39604543Smarks priv_freeset(priv_effective); 39614543Smarks 39624543Smarks if (privileged) { 39634543Smarks if ((dhdl = di_devlink_init(ZFS_DRIVER, 39644543Smarks DI_MAKE_LINK)) == NULL) { 39654543Smarks zfs_error_aux(hdl, strerror(errno)); 39664543Smarks (void) zfs_standard_error_fmt(hdl, EZFS_DEVLINKS, 39674543Smarks dgettext(TEXT_DOMAIN, "cannot create device links " 39684543Smarks "for '%s'"), dataset); 39694543Smarks (void) ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc); 39704543Smarks return (-1); 39714543Smarks } else { 39724543Smarks (void) di_devlink_fini(&dhdl); 39734543Smarks } 3974789Sahrens } else { 39754543Smarks char pathname[MAXPATHLEN]; 39764543Smarks struct stat64 statbuf; 39774543Smarks int i; 39784543Smarks 39794543Smarks #define MAX_WAIT 10 39804543Smarks 39814543Smarks /* 39824543Smarks * This is the poor mans way of waiting for the link 39834543Smarks * to show up. If after 10 seconds we still don't 39844543Smarks * have it, then print out a message. 39854543Smarks */ 39864543Smarks (void) snprintf(pathname, sizeof (pathname), "/dev/zvol/dsk/%s", 39874543Smarks dataset); 39884543Smarks 39894543Smarks for (i = 0; i != MAX_WAIT; i++) { 39904543Smarks if (stat64(pathname, &statbuf) == 0) 39914543Smarks break; 39924543Smarks (void) sleep(1); 39934543Smarks } 39944543Smarks if (i == MAX_WAIT) 39954543Smarks (void) printf(gettext("%s may not be immediately " 39964543Smarks "available\n"), pathname); 3997789Sahrens } 3998789Sahrens 3999789Sahrens return (0); 4000789Sahrens } 4001789Sahrens 4002789Sahrens /* 4003789Sahrens * Remove a minor node for the given zvol and the associated /dev links. 4004789Sahrens */ 4005789Sahrens int 40062082Seschrock zvol_remove_link(libzfs_handle_t *hdl, const char *dataset) 4007789Sahrens { 4008789Sahrens zfs_cmd_t zc = { 0 }; 4009789Sahrens 4010789Sahrens (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 4011789Sahrens 40122082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) { 4013789Sahrens switch (errno) { 4014789Sahrens case ENXIO: 4015789Sahrens /* 4016789Sahrens * Silently ignore the case where the link no longer 4017789Sahrens * exists, so that 'zfs volfini' can be run multiple 4018789Sahrens * times without errors. 4019789Sahrens */ 4020789Sahrens return (0); 4021789Sahrens 4022789Sahrens default: 40233237Slling return (zfs_standard_error_fmt(hdl, errno, 40242082Seschrock dgettext(TEXT_DOMAIN, "cannot remove device " 40252082Seschrock "links for '%s'"), dataset)); 4026789Sahrens } 4027789Sahrens } 4028789Sahrens 4029789Sahrens return (0); 4030789Sahrens } 40312676Seschrock 40322676Seschrock nvlist_t * 40332676Seschrock zfs_get_user_props(zfs_handle_t *zhp) 40342676Seschrock { 40352676Seschrock return (zhp->zfs_user_props); 40362676Seschrock } 40372676Seschrock 40382676Seschrock /* 40393912Slling * This function is used by 'zfs list' to determine the exact set of columns to 40403912Slling * display, and their maximum widths. This does two main things: 40413912Slling * 40423912Slling * - If this is a list of all properties, then expand the list to include 40433912Slling * all native properties, and set a flag so that for each dataset we look 40443912Slling * for new unique user properties and add them to the list. 40453912Slling * 40463912Slling * - For non fixed-width properties, keep track of the maximum width seen 40473912Slling * so that we can size the column appropriately. 40483912Slling */ 40493912Slling int 4050*5094Slling zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp) 40513912Slling { 40523912Slling libzfs_handle_t *hdl = zhp->zfs_hdl; 4053*5094Slling zprop_list_t *entry; 4054*5094Slling zprop_list_t **last, **start; 40553912Slling nvlist_t *userprops, *propval; 40563912Slling nvpair_t *elem; 40573912Slling char *strval; 40583912Slling char buf[ZFS_MAXPROPLEN]; 40593912Slling 4060*5094Slling if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0) 40613912Slling return (-1); 40622676Seschrock 40632676Seschrock userprops = zfs_get_user_props(zhp); 40642676Seschrock 40652676Seschrock entry = *plp; 40662676Seschrock if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) { 40672676Seschrock /* 40682676Seschrock * Go through and add any user properties as necessary. We 40692676Seschrock * start by incrementing our list pointer to the first 40702676Seschrock * non-native property. 40712676Seschrock */ 40722676Seschrock start = plp; 40732676Seschrock while (*start != NULL) { 4074*5094Slling if ((*start)->pl_prop == ZPROP_INVAL) 40752676Seschrock break; 40762676Seschrock start = &(*start)->pl_next; 40772676Seschrock } 40782676Seschrock 40792676Seschrock elem = NULL; 40802676Seschrock while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) { 40812676Seschrock /* 40822676Seschrock * See if we've already found this property in our list. 40832676Seschrock */ 40842676Seschrock for (last = start; *last != NULL; 40852676Seschrock last = &(*last)->pl_next) { 40862676Seschrock if (strcmp((*last)->pl_user_prop, 40872676Seschrock nvpair_name(elem)) == 0) 40882676Seschrock break; 40892676Seschrock } 40902676Seschrock 40912676Seschrock if (*last == NULL) { 40922676Seschrock if ((entry = zfs_alloc(hdl, 4093*5094Slling sizeof (zprop_list_t))) == NULL || 40942676Seschrock ((entry->pl_user_prop = zfs_strdup(hdl, 40952676Seschrock nvpair_name(elem)))) == NULL) { 40962676Seschrock free(entry); 40972676Seschrock return (-1); 40982676Seschrock } 40992676Seschrock 4100*5094Slling entry->pl_prop = ZPROP_INVAL; 41012676Seschrock entry->pl_width = strlen(nvpair_name(elem)); 41022676Seschrock entry->pl_all = B_TRUE; 41032676Seschrock *last = entry; 41042676Seschrock } 41052676Seschrock } 41062676Seschrock } 41072676Seschrock 41082676Seschrock /* 41092676Seschrock * Now go through and check the width of any non-fixed columns 41102676Seschrock */ 41112676Seschrock for (entry = *plp; entry != NULL; entry = entry->pl_next) { 41122676Seschrock if (entry->pl_fixed) 41132676Seschrock continue; 41142676Seschrock 4115*5094Slling if (entry->pl_prop != ZPROP_INVAL) { 41162676Seschrock if (zfs_prop_get(zhp, entry->pl_prop, 41172676Seschrock buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) { 41182676Seschrock if (strlen(buf) > entry->pl_width) 41192676Seschrock entry->pl_width = strlen(buf); 41202676Seschrock } 41212676Seschrock } else if (nvlist_lookup_nvlist(userprops, 41222676Seschrock entry->pl_user_prop, &propval) == 0) { 41232676Seschrock verify(nvlist_lookup_string(propval, 4124*5094Slling ZPROP_VALUE, &strval) == 0); 41252676Seschrock if (strlen(strval) > entry->pl_width) 41262676Seschrock entry->pl_width = strlen(strval); 41272676Seschrock } 41282676Seschrock } 41292676Seschrock 41302676Seschrock return (0); 41312676Seschrock } 41324543Smarks 41334543Smarks int 41344543Smarks zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred) 41354543Smarks { 41364543Smarks zfs_cmd_t zc = { 0 }; 41374543Smarks nvlist_t *nvp; 41384543Smarks gid_t gid; 41394543Smarks uid_t uid; 41404543Smarks const gid_t *groups; 41414543Smarks int group_cnt; 41424543Smarks int error; 41434543Smarks 41444543Smarks if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0) 41454543Smarks return (no_memory(hdl)); 41464543Smarks 41474543Smarks uid = ucred_geteuid(cred); 41484543Smarks gid = ucred_getegid(cred); 41494543Smarks group_cnt = ucred_getgroups(cred, &groups); 41504543Smarks 41514543Smarks if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1) 41524543Smarks return (1); 41534543Smarks 41544543Smarks if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) { 41554543Smarks nvlist_free(nvp); 41564543Smarks return (1); 41574543Smarks } 41584543Smarks 41594543Smarks if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) { 41604543Smarks nvlist_free(nvp); 41614543Smarks return (1); 41624543Smarks } 41634543Smarks 41644543Smarks if (nvlist_add_uint32_array(nvp, 41654543Smarks ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) { 41664543Smarks nvlist_free(nvp); 41674543Smarks return (1); 41684543Smarks } 41694543Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 41704543Smarks 4171*5094Slling if (zcmd_write_src_nvlist(hdl, &zc, nvp)) 41724543Smarks return (-1); 41734543Smarks 41744543Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc); 41754543Smarks nvlist_free(nvp); 41764543Smarks return (error); 41774543Smarks } 41784543Smarks 41794543Smarks int 41804543Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path, 41814543Smarks void *export, void *sharetab, int sharemax, boolean_t share_on) 41824543Smarks { 41834543Smarks zfs_cmd_t zc = { 0 }; 41844543Smarks int error; 41854543Smarks 41864543Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 41874543Smarks (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value)); 41884543Smarks zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab; 41894543Smarks zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export; 41904543Smarks zc.zc_share.z_sharetype = share_on; 41914543Smarks zc.zc_share.z_sharemax = sharemax; 41924543Smarks 41934543Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc); 41944543Smarks return (error); 41954543Smarks } 4196