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> 54*5331Samw #include <sys/zfs_i18n.h> 55789Sahrens #include <libzfs.h> 56789Sahrens 57789Sahrens #include "zfs_namecheck.h" 58789Sahrens #include "zfs_prop.h" 59789Sahrens #include "libzfs_impl.h" 604543Smarks #include "zfs_deleg.h" 61789Sahrens 624490Svb160487 static int create_parents(libzfs_handle_t *, char *, int); 634007Smmusante static int zvol_create_link_common(libzfs_handle_t *, const char *, int); 644007Smmusante 65789Sahrens /* 66789Sahrens * Given a single type (not a mask of types), return the type in a human 67789Sahrens * readable form. 68789Sahrens */ 69789Sahrens const char * 70789Sahrens zfs_type_to_name(zfs_type_t type) 71789Sahrens { 72789Sahrens switch (type) { 73789Sahrens case ZFS_TYPE_FILESYSTEM: 74789Sahrens return (dgettext(TEXT_DOMAIN, "filesystem")); 75789Sahrens case ZFS_TYPE_SNAPSHOT: 76789Sahrens return (dgettext(TEXT_DOMAIN, "snapshot")); 77789Sahrens case ZFS_TYPE_VOLUME: 78789Sahrens return (dgettext(TEXT_DOMAIN, "volume")); 79789Sahrens } 80789Sahrens 81789Sahrens return (NULL); 82789Sahrens } 83789Sahrens 84789Sahrens /* 85789Sahrens * Given a path and mask of ZFS types, return a string describing this dataset. 86789Sahrens * This is used when we fail to open a dataset and we cannot get an exact type. 87789Sahrens * We guess what the type would have been based on the path and the mask of 88789Sahrens * acceptable types. 89789Sahrens */ 90789Sahrens static const char * 91789Sahrens path_to_str(const char *path, int types) 92789Sahrens { 93789Sahrens /* 94789Sahrens * When given a single type, always report the exact type. 95789Sahrens */ 96789Sahrens if (types == ZFS_TYPE_SNAPSHOT) 97789Sahrens return (dgettext(TEXT_DOMAIN, "snapshot")); 98789Sahrens if (types == ZFS_TYPE_FILESYSTEM) 99789Sahrens return (dgettext(TEXT_DOMAIN, "filesystem")); 100789Sahrens if (types == ZFS_TYPE_VOLUME) 101789Sahrens return (dgettext(TEXT_DOMAIN, "volume")); 102789Sahrens 103789Sahrens /* 104789Sahrens * The user is requesting more than one type of dataset. If this is the 105789Sahrens * case, consult the path itself. If we're looking for a snapshot, and 106789Sahrens * a '@' is found, then report it as "snapshot". Otherwise, remove the 107789Sahrens * snapshot attribute and try again. 108789Sahrens */ 109789Sahrens if (types & ZFS_TYPE_SNAPSHOT) { 110789Sahrens if (strchr(path, '@') != NULL) 111789Sahrens return (dgettext(TEXT_DOMAIN, "snapshot")); 112789Sahrens return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT)); 113789Sahrens } 114789Sahrens 115789Sahrens 116789Sahrens /* 117789Sahrens * The user has requested either filesystems or volumes. 118789Sahrens * We have no way of knowing a priori what type this would be, so always 119789Sahrens * report it as "filesystem" or "volume", our two primitive types. 120789Sahrens */ 121789Sahrens if (types & ZFS_TYPE_FILESYSTEM) 122789Sahrens return (dgettext(TEXT_DOMAIN, "filesystem")); 123789Sahrens 124789Sahrens assert(types & ZFS_TYPE_VOLUME); 125789Sahrens return (dgettext(TEXT_DOMAIN, "volume")); 126789Sahrens } 127789Sahrens 128789Sahrens /* 129789Sahrens * Validate a ZFS path. This is used even before trying to open the dataset, to 130789Sahrens * provide a more meaningful error message. We place a more useful message in 131789Sahrens * 'buf' detailing exactly why the name was not valid. 132789Sahrens */ 133789Sahrens static int 1345326Sek110237 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type, 1355326Sek110237 boolean_t modifying) 136789Sahrens { 137789Sahrens namecheck_err_t why; 138789Sahrens char what; 139789Sahrens 140789Sahrens if (dataset_namecheck(path, &why, &what) != 0) { 1412082Seschrock if (hdl != NULL) { 142789Sahrens switch (why) { 1431003Slling case NAME_ERR_TOOLONG: 1442082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1452082Seschrock "name is too long")); 1461003Slling break; 1471003Slling 148789Sahrens case NAME_ERR_LEADING_SLASH: 1492082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1502082Seschrock "leading slash in name")); 151789Sahrens break; 152789Sahrens 153789Sahrens case NAME_ERR_EMPTY_COMPONENT: 1542082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1552082Seschrock "empty component in name")); 156789Sahrens break; 157789Sahrens 158789Sahrens case NAME_ERR_TRAILING_SLASH: 1592082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1602082Seschrock "trailing slash in name")); 161789Sahrens break; 162789Sahrens 163789Sahrens case NAME_ERR_INVALCHAR: 1642082Seschrock zfs_error_aux(hdl, 165789Sahrens dgettext(TEXT_DOMAIN, "invalid character " 1662082Seschrock "'%c' in name"), what); 167789Sahrens break; 168789Sahrens 169789Sahrens case NAME_ERR_MULTIPLE_AT: 1702082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1712082Seschrock "multiple '@' delimiters in name")); 172789Sahrens break; 1732856Snd150628 1742856Snd150628 case NAME_ERR_NOLETTER: 1752856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1762856Snd150628 "pool doesn't begin with a letter")); 1772856Snd150628 break; 1782856Snd150628 1792856Snd150628 case NAME_ERR_RESERVED: 1802856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1812856Snd150628 "name is reserved")); 1822856Snd150628 break; 1832856Snd150628 1842856Snd150628 case NAME_ERR_DISKLIKE: 1852856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1862856Snd150628 "reserved disk name")); 1872856Snd150628 break; 188789Sahrens } 189789Sahrens } 190789Sahrens 191789Sahrens return (0); 192789Sahrens } 193789Sahrens 194789Sahrens if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) { 1952082Seschrock if (hdl != NULL) 1962082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1972082Seschrock "snapshot delimiter '@' in filesystem name")); 198789Sahrens return (0); 199789Sahrens } 200789Sahrens 2012199Sahrens if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) { 2022199Sahrens if (hdl != NULL) 2032199Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2043413Smmusante "missing '@' delimiter in snapshot name")); 2052199Sahrens return (0); 2062199Sahrens } 2072199Sahrens 2085326Sek110237 if (modifying && strchr(path, '%') != NULL) { 2095326Sek110237 if (hdl != NULL) 2105326Sek110237 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2115326Sek110237 "invalid character %c in name"), '%'); 2125326Sek110237 return (0); 2135326Sek110237 } 2145326Sek110237 2152082Seschrock return (-1); 216789Sahrens } 217789Sahrens 218789Sahrens int 219789Sahrens zfs_name_valid(const char *name, zfs_type_t type) 220789Sahrens { 2215326Sek110237 return (zfs_validate_name(NULL, name, type, B_FALSE)); 222789Sahrens } 223789Sahrens 224789Sahrens /* 2252676Seschrock * This function takes the raw DSL properties, and filters out the user-defined 2262676Seschrock * properties into a separate nvlist. 2272676Seschrock */ 2284217Seschrock static nvlist_t * 2294217Seschrock process_user_props(zfs_handle_t *zhp, nvlist_t *props) 2302676Seschrock { 2312676Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 2322676Seschrock nvpair_t *elem; 2332676Seschrock nvlist_t *propval; 2344217Seschrock nvlist_t *nvl; 2354217Seschrock 2364217Seschrock if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) { 2374217Seschrock (void) no_memory(hdl); 2384217Seschrock return (NULL); 2394217Seschrock } 2402676Seschrock 2412676Seschrock elem = NULL; 2424217Seschrock while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 2432676Seschrock if (!zfs_prop_user(nvpair_name(elem))) 2442676Seschrock continue; 2452676Seschrock 2462676Seschrock verify(nvpair_value_nvlist(elem, &propval) == 0); 2474217Seschrock if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) { 2484217Seschrock nvlist_free(nvl); 2494217Seschrock (void) no_memory(hdl); 2504217Seschrock return (NULL); 2514217Seschrock } 2522676Seschrock } 2532676Seschrock 2544217Seschrock return (nvl); 2552676Seschrock } 2562676Seschrock 2572676Seschrock /* 258789Sahrens * Utility function to gather stats (objset and zpl) for the given object. 259789Sahrens */ 260789Sahrens static int 261789Sahrens get_stats(zfs_handle_t *zhp) 262789Sahrens { 263789Sahrens zfs_cmd_t zc = { 0 }; 2642676Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 2654217Seschrock nvlist_t *allprops, *userprops; 266789Sahrens 267789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 268789Sahrens 2692676Seschrock if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) 2702082Seschrock return (-1); 2711356Seschrock 2722082Seschrock while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) { 2731356Seschrock if (errno == ENOMEM) { 2742676Seschrock if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 2752676Seschrock zcmd_free_nvlists(&zc); 2762082Seschrock return (-1); 2772676Seschrock } 2781356Seschrock } else { 2792676Seschrock zcmd_free_nvlists(&zc); 2801356Seschrock return (-1); 2811356Seschrock } 2821356Seschrock } 283789Sahrens 2842885Sahrens zhp->zfs_dmustats = zc.zc_objset_stats; /* structure assignment */ 285789Sahrens 2862676Seschrock (void) strlcpy(zhp->zfs_root, zc.zc_value, sizeof (zhp->zfs_root)); 2871544Seschrock 2884217Seschrock if (zcmd_read_dst_nvlist(hdl, &zc, &allprops) != 0) { 2892676Seschrock zcmd_free_nvlists(&zc); 2902082Seschrock return (-1); 2912082Seschrock } 292789Sahrens 2932676Seschrock zcmd_free_nvlists(&zc); 2942676Seschrock 2954217Seschrock if ((userprops = process_user_props(zhp, allprops)) == NULL) { 2964217Seschrock nvlist_free(allprops); 2972676Seschrock return (-1); 2984217Seschrock } 2994217Seschrock 3004217Seschrock nvlist_free(zhp->zfs_props); 3014217Seschrock nvlist_free(zhp->zfs_user_props); 3024217Seschrock 3034217Seschrock zhp->zfs_props = allprops; 3044217Seschrock zhp->zfs_user_props = userprops; 3052082Seschrock 306789Sahrens return (0); 307789Sahrens } 308789Sahrens 309789Sahrens /* 310789Sahrens * Refresh the properties currently stored in the handle. 311789Sahrens */ 312789Sahrens void 313789Sahrens zfs_refresh_properties(zfs_handle_t *zhp) 314789Sahrens { 315789Sahrens (void) get_stats(zhp); 316789Sahrens } 317789Sahrens 318789Sahrens /* 319789Sahrens * Makes a handle from the given dataset name. Used by zfs_open() and 320789Sahrens * zfs_iter_* to create child handles on the fly. 321789Sahrens */ 322789Sahrens zfs_handle_t * 3232082Seschrock make_dataset_handle(libzfs_handle_t *hdl, const char *path) 324789Sahrens { 3252082Seschrock zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 3264543Smarks char *logstr; 3272082Seschrock 3282082Seschrock if (zhp == NULL) 3292082Seschrock return (NULL); 3302082Seschrock 3312082Seschrock zhp->zfs_hdl = hdl; 332789Sahrens 3334543Smarks /* 3344543Smarks * Preserve history log string. 3354543Smarks * any changes performed here will be 3364543Smarks * logged as an internal event. 3374543Smarks */ 3384543Smarks logstr = zhp->zfs_hdl->libzfs_log_str; 3394543Smarks zhp->zfs_hdl->libzfs_log_str = NULL; 3401758Sahrens top: 341789Sahrens (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name)); 342789Sahrens 343789Sahrens if (get_stats(zhp) != 0) { 3444543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 345789Sahrens free(zhp); 346789Sahrens return (NULL); 347789Sahrens } 348789Sahrens 3491758Sahrens if (zhp->zfs_dmustats.dds_inconsistent) { 3501758Sahrens zfs_cmd_t zc = { 0 }; 3511758Sahrens 3521758Sahrens /* 3531758Sahrens * If it is dds_inconsistent, then we've caught it in 3541758Sahrens * the middle of a 'zfs receive' or 'zfs destroy', and 3551758Sahrens * it is inconsistent from the ZPL's point of view, so 3561758Sahrens * can't be mounted. However, it could also be that we 3571758Sahrens * have crashed in the middle of one of those 3581758Sahrens * operations, in which case we need to get rid of the 3591758Sahrens * inconsistent state. We do that by either rolling 3601758Sahrens * back to the previous snapshot (which will fail if 3611758Sahrens * there is none), or destroying the filesystem. Note 3621758Sahrens * that if we are still in the middle of an active 3631758Sahrens * 'receive' or 'destroy', then the rollback and destroy 3641758Sahrens * will fail with EBUSY and we will drive on as usual. 3651758Sahrens */ 3661758Sahrens 3671758Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3681758Sahrens 3692885Sahrens if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) { 3702082Seschrock (void) zvol_remove_link(hdl, zhp->zfs_name); 3711758Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3721758Sahrens } else { 3731758Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3741758Sahrens } 3751758Sahrens 3761758Sahrens /* If we can successfully roll it back, reget the stats */ 3772082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_ROLLBACK, &zc) == 0) 3781758Sahrens goto top; 3791758Sahrens /* 380*5331Samw * If we can successfully destroy it, pretend that it 3811758Sahrens * never existed. 3821758Sahrens */ 3832082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc) == 0) { 3844543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 3851758Sahrens free(zhp); 3861758Sahrens errno = ENOENT; 3871758Sahrens return (NULL); 3881758Sahrens } 3891758Sahrens } 3901758Sahrens 391789Sahrens /* 392789Sahrens * We've managed to open the dataset and gather statistics. Determine 393789Sahrens * the high-level type. 394789Sahrens */ 3952885Sahrens if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) 3962885Sahrens zhp->zfs_head_type = ZFS_TYPE_VOLUME; 3972885Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) 3982885Sahrens zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM; 3992885Sahrens else 4002885Sahrens abort(); 4012885Sahrens 402789Sahrens if (zhp->zfs_dmustats.dds_is_snapshot) 403789Sahrens zhp->zfs_type = ZFS_TYPE_SNAPSHOT; 404789Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) 405789Sahrens zhp->zfs_type = ZFS_TYPE_VOLUME; 406789Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) 407789Sahrens zhp->zfs_type = ZFS_TYPE_FILESYSTEM; 408789Sahrens else 4092082Seschrock abort(); /* we should never see any other types */ 410789Sahrens 4114543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 412789Sahrens return (zhp); 413789Sahrens } 414789Sahrens 415789Sahrens /* 416789Sahrens * Opens the given snapshot, filesystem, or volume. The 'types' 417789Sahrens * argument is a mask of acceptable types. The function will print an 418789Sahrens * appropriate error message and return NULL if it can't be opened. 419789Sahrens */ 420789Sahrens zfs_handle_t * 4212082Seschrock zfs_open(libzfs_handle_t *hdl, const char *path, int types) 422789Sahrens { 423789Sahrens zfs_handle_t *zhp; 4242082Seschrock char errbuf[1024]; 4252082Seschrock 4262082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 4272082Seschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), path); 428789Sahrens 429789Sahrens /* 4302082Seschrock * Validate the name before we even try to open it. 431789Sahrens */ 4325326Sek110237 if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) { 4332082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4342082Seschrock "invalid dataset name")); 4352082Seschrock (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 436789Sahrens return (NULL); 437789Sahrens } 438789Sahrens 439789Sahrens /* 440789Sahrens * Try to get stats for the dataset, which will tell us if it exists. 441789Sahrens */ 442789Sahrens errno = 0; 4432082Seschrock if ((zhp = make_dataset_handle(hdl, path)) == NULL) { 4443237Slling (void) zfs_standard_error(hdl, errno, errbuf); 445789Sahrens return (NULL); 446789Sahrens } 447789Sahrens 448789Sahrens if (!(types & zhp->zfs_type)) { 4492082Seschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 4502142Seschrock zfs_close(zhp); 451789Sahrens return (NULL); 452789Sahrens } 453789Sahrens 454789Sahrens return (zhp); 455789Sahrens } 456789Sahrens 457789Sahrens /* 458789Sahrens * Release a ZFS handle. Nothing to do but free the associated memory. 459789Sahrens */ 460789Sahrens void 461789Sahrens zfs_close(zfs_handle_t *zhp) 462789Sahrens { 463789Sahrens if (zhp->zfs_mntopts) 464789Sahrens free(zhp->zfs_mntopts); 4652676Seschrock nvlist_free(zhp->zfs_props); 4662676Seschrock nvlist_free(zhp->zfs_user_props); 467789Sahrens free(zhp); 468789Sahrens } 469789Sahrens 4703912Slling 4713912Slling /* 4722676Seschrock * Given an nvlist of properties to set, validates that they are correct, and 4732676Seschrock * parses any numeric properties (index, boolean, etc) if they are specified as 4742676Seschrock * strings. 475789Sahrens */ 4765094Slling static nvlist_t * 4775094Slling zfs_validate_properties(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl, 4785094Slling uint64_t zoned, zfs_handle_t *zhp, const char *errbuf) 479789Sahrens { 4802676Seschrock nvpair_t *elem; 4812676Seschrock uint64_t intval; 4822676Seschrock char *strval; 4835094Slling zfs_prop_t prop; 4842676Seschrock nvlist_t *ret; 485*5331Samw int chosen_normal = -1; 486*5331Samw int chosen_utf = -1; 4872676Seschrock 4882676Seschrock if (type == ZFS_TYPE_SNAPSHOT) { 4892676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4903413Smmusante "snapshot properties cannot be modified")); 4912676Seschrock (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 4925094Slling return (NULL); 4935094Slling } 4945094Slling 4955094Slling if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) { 4965094Slling (void) no_memory(hdl); 4975094Slling return (NULL); 498789Sahrens } 499789Sahrens 5002676Seschrock elem = NULL; 5012676Seschrock while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { 5025094Slling const char *propname = nvpair_name(elem); 5032676Seschrock 5042676Seschrock /* 5052676Seschrock * Make sure this property is valid and applies to this type. 5062676Seschrock */ 5075094Slling if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) { 5085094Slling if (!zfs_prop_user(propname)) { 5092676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5105094Slling "invalid property '%s'"), propname); 5115094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 5125094Slling goto error; 5135094Slling } 5145094Slling 5155094Slling /* 5165094Slling * If this is a user property, make sure it's a 5175094Slling * string, and that it's less than ZAP_MAXNAMELEN. 5185094Slling */ 5195094Slling if (nvpair_type(elem) != DATA_TYPE_STRING) { 5205094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5215094Slling "'%s' must be a string"), propname); 5225094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 5235094Slling goto error; 5245094Slling } 5255094Slling 5265094Slling if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) { 5275094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5285094Slling "property name '%s' is too long"), 5292676Seschrock propname); 5302676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 5312676Seschrock goto error; 5322676Seschrock } 5332676Seschrock 5342676Seschrock (void) nvpair_value_string(elem, &strval); 5352676Seschrock if (nvlist_add_string(ret, propname, strval) != 0) { 5362676Seschrock (void) no_memory(hdl); 5372676Seschrock goto error; 5382676Seschrock } 5392676Seschrock continue; 540789Sahrens } 5412676Seschrock 5422676Seschrock if (!zfs_prop_valid_for_type(prop, type)) { 5432676Seschrock zfs_error_aux(hdl, 5442676Seschrock dgettext(TEXT_DOMAIN, "'%s' does not " 5452676Seschrock "apply to datasets of this type"), propname); 5462676Seschrock (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 5472676Seschrock goto error; 5482676Seschrock } 5492676Seschrock 5502676Seschrock if (zfs_prop_readonly(prop) && 551*5331Samw (!zfs_prop_setonce(prop) || zhp != NULL)) { 5522676Seschrock zfs_error_aux(hdl, 5532676Seschrock dgettext(TEXT_DOMAIN, "'%s' is readonly"), 5542676Seschrock propname); 5552676Seschrock (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 5562676Seschrock goto error; 5572676Seschrock } 5582676Seschrock 5595094Slling if (zprop_parse_value(hdl, elem, prop, type, ret, 5605094Slling &strval, &intval, errbuf) != 0) 5612676Seschrock goto error; 5622676Seschrock 5632676Seschrock /* 5642676Seschrock * Perform some additional checks for specific properties. 5652676Seschrock */ 5662676Seschrock switch (prop) { 5674577Sahrens case ZFS_PROP_VERSION: 5684577Sahrens { 5694577Sahrens int version; 5704577Sahrens 5714577Sahrens if (zhp == NULL) 5724577Sahrens break; 5734577Sahrens version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION); 5744577Sahrens if (intval < version) { 5754577Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5764577Sahrens "Can not downgrade; already at version %u"), 5774577Sahrens version); 5784577Sahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 5794577Sahrens goto error; 5804577Sahrens } 5814577Sahrens break; 5824577Sahrens } 5834577Sahrens 5842676Seschrock case ZFS_PROP_RECORDSIZE: 5852676Seschrock case ZFS_PROP_VOLBLOCKSIZE: 5862676Seschrock /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */ 5872676Seschrock if (intval < SPA_MINBLOCKSIZE || 5882676Seschrock intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) { 5892082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5902676Seschrock "'%s' must be power of 2 from %u " 5912676Seschrock "to %uk"), propname, 5922676Seschrock (uint_t)SPA_MINBLOCKSIZE, 5932676Seschrock (uint_t)SPA_MAXBLOCKSIZE >> 10); 5942676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 5952676Seschrock goto error; 596789Sahrens } 597789Sahrens break; 598789Sahrens 5993126Sahl case ZFS_PROP_SHAREISCSI: 6003126Sahl if (strcmp(strval, "off") != 0 && 6013126Sahl strcmp(strval, "on") != 0 && 6023126Sahl strcmp(strval, "type=disk") != 0) { 6033126Sahl zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6043126Sahl "'%s' must be 'on', 'off', or 'type=disk'"), 6053126Sahl propname); 6063126Sahl (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 6073126Sahl goto error; 6083126Sahl } 6093126Sahl 6103126Sahl break; 6113126Sahl 6122676Seschrock case ZFS_PROP_MOUNTPOINT: 6134778Srm160521 { 6144778Srm160521 namecheck_err_t why; 6154778Srm160521 6162676Seschrock if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 || 6172676Seschrock strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0) 6182676Seschrock break; 6192676Seschrock 6204778Srm160521 if (mountpoint_namecheck(strval, &why)) { 6214778Srm160521 switch (why) { 6224778Srm160521 case NAME_ERR_LEADING_SLASH: 6234778Srm160521 zfs_error_aux(hdl, 6244778Srm160521 dgettext(TEXT_DOMAIN, 6254778Srm160521 "'%s' must be an absolute path, " 6264778Srm160521 "'none', or 'legacy'"), propname); 6274778Srm160521 break; 6284778Srm160521 case NAME_ERR_TOOLONG: 6294778Srm160521 zfs_error_aux(hdl, 6304778Srm160521 dgettext(TEXT_DOMAIN, 6314778Srm160521 "component of '%s' is too long"), 6324778Srm160521 propname); 6334778Srm160521 break; 6344778Srm160521 } 6352676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 6362676Seschrock goto error; 637789Sahrens } 6384778Srm160521 } 6394778Srm160521 6403126Sahl /*FALLTHRU*/ 6413126Sahl 642*5331Samw case ZFS_PROP_SHARESMB: 6433126Sahl case ZFS_PROP_SHARENFS: 6443126Sahl /* 645*5331Samw * For the mountpoint and sharenfs or sharesmb 646*5331Samw * properties, check if it can be set in a 647*5331Samw * global/non-global zone based on 6483126Sahl * the zoned property value: 6493126Sahl * 6503126Sahl * global zone non-global zone 6513126Sahl * -------------------------------------------------- 6523126Sahl * zoned=on mountpoint (no) mountpoint (yes) 6533126Sahl * sharenfs (no) sharenfs (no) 654*5331Samw * sharesmb (no) sharesmb (no) 6553126Sahl * 6563126Sahl * zoned=off mountpoint (yes) N/A 6573126Sahl * sharenfs (yes) 658*5331Samw * sharesmb (yes) 6593126Sahl */ 6602676Seschrock if (zoned) { 6612676Seschrock if (getzoneid() == GLOBAL_ZONEID) { 6622676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6632676Seschrock "'%s' cannot be set on " 6642676Seschrock "dataset in a non-global zone"), 6652676Seschrock propname); 6662676Seschrock (void) zfs_error(hdl, EZFS_ZONED, 6672676Seschrock errbuf); 6682676Seschrock goto error; 669*5331Samw } else if (prop == ZFS_PROP_SHARENFS || 670*5331Samw prop == ZFS_PROP_SHARESMB) { 6712676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6722676Seschrock "'%s' cannot be set in " 6732676Seschrock "a non-global zone"), propname); 6742676Seschrock (void) zfs_error(hdl, EZFS_ZONED, 6752676Seschrock errbuf); 6762676Seschrock goto error; 6772676Seschrock } 6782676Seschrock } else if (getzoneid() != GLOBAL_ZONEID) { 6792676Seschrock /* 6802676Seschrock * If zoned property is 'off', this must be in 6812676Seschrock * a globle zone. If not, something is wrong. 6822676Seschrock */ 6832676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6842676Seschrock "'%s' cannot be set while dataset " 6852676Seschrock "'zoned' property is set"), propname); 6862676Seschrock (void) zfs_error(hdl, EZFS_ZONED, errbuf); 6872676Seschrock goto error; 6882676Seschrock } 6893126Sahl 6904180Sdougm /* 6914180Sdougm * At this point, it is legitimate to set the 6924180Sdougm * property. Now we want to make sure that the 6934180Sdougm * property value is valid if it is sharenfs. 6944180Sdougm */ 695*5331Samw if ((prop == ZFS_PROP_SHARENFS || 696*5331Samw prop == ZFS_PROP_SHARESMB) && 6974217Seschrock strcmp(strval, "on") != 0 && 6984217Seschrock strcmp(strval, "off") != 0) { 699*5331Samw zfs_share_proto_t proto; 700*5331Samw 701*5331Samw if (prop == ZFS_PROP_SHARESMB) 702*5331Samw proto = PROTO_SMB; 703*5331Samw else 704*5331Samw proto = PROTO_NFS; 7054180Sdougm 7064180Sdougm /* 707*5331Samw * Must be an valid sharing protocol 708*5331Samw * option string so init the libshare 709*5331Samw * in order to enable the parser and 710*5331Samw * then parse the options. We use the 711*5331Samw * control API since we don't care about 712*5331Samw * the current configuration and don't 7134180Sdougm * want the overhead of loading it 7144180Sdougm * until we actually do something. 7154180Sdougm */ 7164180Sdougm 7174217Seschrock if (zfs_init_libshare(hdl, 7184217Seschrock SA_INIT_CONTROL_API) != SA_OK) { 7194217Seschrock /* 7204217Seschrock * An error occurred so we can't do 7214217Seschrock * anything 7224217Seschrock */ 7234217Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7244217Seschrock "'%s' cannot be set: problem " 7254217Seschrock "in share initialization"), 7264217Seschrock propname); 7274217Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 7284217Seschrock errbuf); 7294217Seschrock goto error; 7304217Seschrock } 7314217Seschrock 732*5331Samw if (zfs_parse_options(strval, proto) != SA_OK) { 7334217Seschrock /* 7344217Seschrock * There was an error in parsing so 7354217Seschrock * deal with it by issuing an error 7364217Seschrock * message and leaving after 7374217Seschrock * uninitializing the the libshare 7384217Seschrock * interface. 7394217Seschrock */ 7404217Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7414217Seschrock "'%s' cannot be set to invalid " 7424217Seschrock "options"), propname); 7434217Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 7444217Seschrock errbuf); 7454217Seschrock zfs_uninit_libshare(hdl); 7464217Seschrock goto error; 7474217Seschrock } 7484180Sdougm zfs_uninit_libshare(hdl); 7494180Sdougm } 7504180Sdougm 7513126Sahl break; 752*5331Samw case ZFS_PROP_UTF8ONLY: 753*5331Samw chosen_utf = (int)intval; 754*5331Samw break; 755*5331Samw case ZFS_PROP_NORMALIZE: 756*5331Samw chosen_normal = (int)intval; 757*5331Samw break; 7582676Seschrock } 7592676Seschrock 7602676Seschrock /* 7612676Seschrock * For changes to existing volumes, we have some additional 7622676Seschrock * checks to enforce. 7632676Seschrock */ 7642676Seschrock if (type == ZFS_TYPE_VOLUME && zhp != NULL) { 7652676Seschrock uint64_t volsize = zfs_prop_get_int(zhp, 7662676Seschrock ZFS_PROP_VOLSIZE); 7672676Seschrock uint64_t blocksize = zfs_prop_get_int(zhp, 7682676Seschrock ZFS_PROP_VOLBLOCKSIZE); 7692676Seschrock char buf[64]; 7702676Seschrock 7712676Seschrock switch (prop) { 7722676Seschrock case ZFS_PROP_RESERVATION: 7732676Seschrock if (intval > volsize) { 7742676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7752676Seschrock "'%s' is greater than current " 7762676Seschrock "volume size"), propname); 7772676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 7782676Seschrock errbuf); 7792676Seschrock goto error; 7802676Seschrock } 7812676Seschrock break; 7822676Seschrock 7832676Seschrock case ZFS_PROP_VOLSIZE: 7842676Seschrock if (intval % blocksize != 0) { 7852676Seschrock zfs_nicenum(blocksize, buf, 7862676Seschrock sizeof (buf)); 7872676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7882676Seschrock "'%s' must be a multiple of " 7892676Seschrock "volume block size (%s)"), 7902676Seschrock propname, buf); 7912676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 7922676Seschrock errbuf); 7932676Seschrock goto error; 7942676Seschrock } 7952676Seschrock 7962676Seschrock if (intval == 0) { 7972676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7982676Seschrock "'%s' cannot be zero"), 7992676Seschrock propname); 8002676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 8012676Seschrock errbuf); 8022676Seschrock goto error; 803789Sahrens } 8043126Sahl break; 805789Sahrens } 806789Sahrens } 807789Sahrens } 808789Sahrens 8092676Seschrock /* 810*5331Samw * If normalization was chosen, but no UTF8 choice was made, 811*5331Samw * enforce rejection of non-UTF8 names. 812*5331Samw * 813*5331Samw * If normalization was chosen, but rejecting non-UTF8 names 814*5331Samw * was explicitly not chosen, it is an error. 815*5331Samw */ 816*5331Samw if (chosen_normal > ZFS_NORMALIZE_NONE && chosen_utf < 0) { 817*5331Samw if (nvlist_add_uint64(ret, 818*5331Samw zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) { 819*5331Samw (void) no_memory(hdl); 820*5331Samw goto error; 821*5331Samw } 822*5331Samw } else if (chosen_normal > ZFS_NORMALIZE_NONE && chosen_utf == 0) { 823*5331Samw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 824*5331Samw "'%s' must be set 'on' if normalization chosen"), 825*5331Samw zfs_prop_to_name(ZFS_PROP_UTF8ONLY)); 826*5331Samw (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 827*5331Samw goto error; 828*5331Samw } 829*5331Samw 830*5331Samw /* 8312676Seschrock * If this is an existing volume, and someone is setting the volsize, 8322676Seschrock * make sure that it matches the reservation, or add it if necessary. 8332676Seschrock */ 8342676Seschrock if (zhp != NULL && type == ZFS_TYPE_VOLUME && 8352676Seschrock nvlist_lookup_uint64(ret, zfs_prop_to_name(ZFS_PROP_VOLSIZE), 8362676Seschrock &intval) == 0) { 8372676Seschrock uint64_t old_volsize = zfs_prop_get_int(zhp, 8382676Seschrock ZFS_PROP_VOLSIZE); 8392676Seschrock uint64_t old_reservation = zfs_prop_get_int(zhp, 8402676Seschrock ZFS_PROP_RESERVATION); 8412676Seschrock uint64_t new_reservation; 8422676Seschrock 8432676Seschrock if (old_volsize == old_reservation && 8442676Seschrock nvlist_lookup_uint64(ret, 8452676Seschrock zfs_prop_to_name(ZFS_PROP_RESERVATION), 8462676Seschrock &new_reservation) != 0) { 8472676Seschrock if (nvlist_add_uint64(ret, 8482676Seschrock zfs_prop_to_name(ZFS_PROP_RESERVATION), 8492676Seschrock intval) != 0) { 8502676Seschrock (void) no_memory(hdl); 8512676Seschrock goto error; 8522676Seschrock } 8532676Seschrock } 8542676Seschrock } 8552676Seschrock 8562676Seschrock return (ret); 8572676Seschrock 8582676Seschrock error: 8592676Seschrock nvlist_free(ret); 8602676Seschrock return (NULL); 861789Sahrens } 862789Sahrens 8634543Smarks static int 8644543Smarks zfs_get_perm_who(const char *who, zfs_deleg_who_type_t *who_type, 8654543Smarks uint64_t *ret_who) 8664543Smarks { 8674543Smarks struct passwd *pwd; 8684543Smarks struct group *grp; 8694543Smarks uid_t id; 8704543Smarks 8714543Smarks if (*who_type == ZFS_DELEG_EVERYONE || *who_type == ZFS_DELEG_CREATE || 8724543Smarks *who_type == ZFS_DELEG_NAMED_SET) { 8734543Smarks *ret_who = -1; 8744543Smarks return (0); 8754543Smarks } 8764543Smarks if (who == NULL && !(*who_type == ZFS_DELEG_EVERYONE)) 8774543Smarks return (EZFS_BADWHO); 8784543Smarks 8794543Smarks if (*who_type == ZFS_DELEG_WHO_UNKNOWN && 8804543Smarks strcmp(who, "everyone") == 0) { 8814543Smarks *ret_who = -1; 8824543Smarks *who_type = ZFS_DELEG_EVERYONE; 8834543Smarks return (0); 8844543Smarks } 8854543Smarks 8864543Smarks pwd = getpwnam(who); 8874543Smarks grp = getgrnam(who); 8884543Smarks 8894543Smarks if ((*who_type == ZFS_DELEG_USER) && pwd) { 8904543Smarks *ret_who = pwd->pw_uid; 8914543Smarks } else if ((*who_type == ZFS_DELEG_GROUP) && grp) { 8924543Smarks *ret_who = grp->gr_gid; 8934543Smarks } else if (pwd) { 8944543Smarks *ret_who = pwd->pw_uid; 8954543Smarks *who_type = ZFS_DELEG_USER; 8964543Smarks } else if (grp) { 8974543Smarks *ret_who = grp->gr_gid; 8984543Smarks *who_type = ZFS_DELEG_GROUP; 8994543Smarks } else { 9004543Smarks char *end; 9014543Smarks 9024543Smarks id = strtol(who, &end, 10); 9034543Smarks if (errno != 0 || *end != '\0') { 9044543Smarks return (EZFS_BADWHO); 9054543Smarks } else { 9064543Smarks *ret_who = id; 9074543Smarks if (*who_type == ZFS_DELEG_WHO_UNKNOWN) 9084543Smarks *who_type = ZFS_DELEG_USER; 9094543Smarks } 9104543Smarks } 9114543Smarks 9124543Smarks return (0); 9134543Smarks } 9144543Smarks 9154543Smarks static void 9164543Smarks zfs_perms_add_to_nvlist(nvlist_t *who_nvp, char *name, nvlist_t *perms_nvp) 9174543Smarks { 9184543Smarks if (perms_nvp != NULL) { 9194543Smarks verify(nvlist_add_nvlist(who_nvp, 9204543Smarks name, perms_nvp) == 0); 9214543Smarks } else { 9224543Smarks verify(nvlist_add_boolean(who_nvp, name) == 0); 9234543Smarks } 9244543Smarks } 9254543Smarks 9264543Smarks static void 9274543Smarks helper(zfs_deleg_who_type_t who_type, uint64_t whoid, char *whostr, 9284543Smarks zfs_deleg_inherit_t inherit, nvlist_t *who_nvp, nvlist_t *perms_nvp, 9294543Smarks nvlist_t *sets_nvp) 9304543Smarks { 9314543Smarks boolean_t do_perms, do_sets; 9324543Smarks char name[ZFS_MAX_DELEG_NAME]; 9334543Smarks 9344543Smarks do_perms = (nvlist_next_nvpair(perms_nvp, NULL) != NULL); 9354543Smarks do_sets = (nvlist_next_nvpair(sets_nvp, NULL) != NULL); 9364543Smarks 9374543Smarks if (!do_perms && !do_sets) 9384543Smarks do_perms = do_sets = B_TRUE; 9394543Smarks 9404543Smarks if (do_perms) { 9414543Smarks zfs_deleg_whokey(name, who_type, inherit, 9424543Smarks (who_type == ZFS_DELEG_NAMED_SET) ? 9434543Smarks whostr : (void *)&whoid); 9444543Smarks zfs_perms_add_to_nvlist(who_nvp, name, perms_nvp); 9454543Smarks } 9464543Smarks if (do_sets) { 9474543Smarks zfs_deleg_whokey(name, toupper(who_type), inherit, 9484543Smarks (who_type == ZFS_DELEG_NAMED_SET) ? 9494543Smarks whostr : (void *)&whoid); 9504543Smarks zfs_perms_add_to_nvlist(who_nvp, name, sets_nvp); 9514543Smarks } 9524543Smarks } 9534543Smarks 9544543Smarks static void 9554543Smarks zfs_perms_add_who_nvlist(nvlist_t *who_nvp, uint64_t whoid, void *whostr, 9564543Smarks nvlist_t *perms_nvp, nvlist_t *sets_nvp, 9574543Smarks zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit) 9584543Smarks { 9594543Smarks if (who_type == ZFS_DELEG_NAMED_SET || who_type == ZFS_DELEG_CREATE) { 9604543Smarks helper(who_type, whoid, whostr, 0, 9614543Smarks who_nvp, perms_nvp, sets_nvp); 9624543Smarks } else { 9634543Smarks if (inherit & ZFS_DELEG_PERM_LOCAL) { 9644543Smarks helper(who_type, whoid, whostr, ZFS_DELEG_LOCAL, 9654543Smarks who_nvp, perms_nvp, sets_nvp); 9664543Smarks } 9674543Smarks if (inherit & ZFS_DELEG_PERM_DESCENDENT) { 9684543Smarks helper(who_type, whoid, whostr, ZFS_DELEG_DESCENDENT, 9694543Smarks who_nvp, perms_nvp, sets_nvp); 9704543Smarks } 9714543Smarks } 9724543Smarks } 9734543Smarks 9744543Smarks /* 9754543Smarks * Construct nvlist to pass down to kernel for setting/removing permissions. 9764543Smarks * 9774543Smarks * The nvlist is constructed as a series of nvpairs with an optional embedded 9784543Smarks * nvlist of permissions to remove or set. The topmost nvpairs are the actual 9794543Smarks * base attribute named stored in the dsl. 9804543Smarks * Arguments: 9814543Smarks * 9824543Smarks * whostr: is a comma separated list of users, groups, or a single set name. 9834543Smarks * whostr may be null for everyone or create perms. 9844543Smarks * who_type: is the type of entry in whostr. Typically this will be 9854543Smarks * ZFS_DELEG_WHO_UNKNOWN. 986*5331Samw * perms: common separated list of permissions. May be null if user 9874543Smarks * is requested to remove permissions by who. 9884543Smarks * inherit: Specifies the inheritance of the permissions. Will be either 9894543Smarks * ZFS_DELEG_PERM_LOCAL and/or ZFS_DELEG_PERM_DESCENDENT. 9904543Smarks * nvp The constructed nvlist to pass to zfs_perm_set(). 9914543Smarks * The output nvp will look something like this. 9924543Smarks * ul$1234 -> {create ; destroy } 9934543Smarks * Ul$1234 -> { @myset } 9944543Smarks * s-$@myset - { snapshot; checksum; compression } 9954543Smarks */ 9964543Smarks int 9974543Smarks zfs_build_perms(zfs_handle_t *zhp, char *whostr, char *perms, 9984543Smarks zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit, nvlist_t **nvp) 9994543Smarks { 10004543Smarks nvlist_t *who_nvp; 10014543Smarks nvlist_t *perms_nvp = NULL; 10024543Smarks nvlist_t *sets_nvp = NULL; 10034543Smarks char errbuf[1024]; 10044787Sahrens char *who_tok, *perm; 10054543Smarks int error; 10064543Smarks 10074543Smarks *nvp = NULL; 10084543Smarks 10094543Smarks if (perms) { 10104543Smarks if ((error = nvlist_alloc(&perms_nvp, 10114543Smarks NV_UNIQUE_NAME, 0)) != 0) { 10124543Smarks return (1); 10134543Smarks } 10144543Smarks if ((error = nvlist_alloc(&sets_nvp, 10154543Smarks NV_UNIQUE_NAME, 0)) != 0) { 10164543Smarks nvlist_free(perms_nvp); 10174543Smarks return (1); 10184543Smarks } 10194543Smarks } 10204543Smarks 10214543Smarks if ((error = nvlist_alloc(&who_nvp, NV_UNIQUE_NAME, 0)) != 0) { 10224543Smarks if (perms_nvp) 10234543Smarks nvlist_free(perms_nvp); 10244543Smarks if (sets_nvp) 10254543Smarks nvlist_free(sets_nvp); 10264543Smarks return (1); 10274543Smarks } 10284543Smarks 10294543Smarks if (who_type == ZFS_DELEG_NAMED_SET) { 10304543Smarks namecheck_err_t why; 10314543Smarks char what; 10324543Smarks 10334543Smarks if ((error = permset_namecheck(whostr, &why, &what)) != 0) { 10344787Sahrens nvlist_free(who_nvp); 10354787Sahrens if (perms_nvp) 10364787Sahrens nvlist_free(perms_nvp); 10374787Sahrens if (sets_nvp) 10384787Sahrens nvlist_free(sets_nvp); 10394787Sahrens 10404543Smarks switch (why) { 10414543Smarks case NAME_ERR_NO_AT: 10424543Smarks zfs_error_aux(zhp->zfs_hdl, 10434543Smarks dgettext(TEXT_DOMAIN, 10444543Smarks "set definition must begin with an '@' " 10454543Smarks "character")); 10464543Smarks } 10474543Smarks return (zfs_error(zhp->zfs_hdl, 10484543Smarks EZFS_BADPERMSET, whostr)); 10494543Smarks } 10504543Smarks } 10514543Smarks 10524543Smarks /* 10534543Smarks * Build up nvlist(s) of permissions. Two nvlists are maintained. 10544543Smarks * The first nvlist perms_nvp will have normal permissions and the 10554543Smarks * other sets_nvp will have only permssion set names in it. 10564543Smarks */ 10574787Sahrens for (perm = strtok(perms, ","); perm; perm = strtok(NULL, ",")) { 10584787Sahrens const char *perm_canonical = zfs_deleg_canonicalize_perm(perm); 10594787Sahrens 10604787Sahrens if (perm_canonical) { 10614787Sahrens verify(nvlist_add_boolean(perms_nvp, 10624787Sahrens perm_canonical) == 0); 10634787Sahrens } else if (perm[0] == '@') { 10644787Sahrens verify(nvlist_add_boolean(sets_nvp, perm) == 0); 10654787Sahrens } else { 10664787Sahrens nvlist_free(who_nvp); 10674787Sahrens nvlist_free(perms_nvp); 10684787Sahrens nvlist_free(sets_nvp); 10694787Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_BADPERM, perm)); 10704543Smarks } 10714543Smarks } 10724543Smarks 10734543Smarks if (whostr && who_type != ZFS_DELEG_CREATE) { 10744543Smarks who_tok = strtok(whostr, ","); 10754543Smarks if (who_tok == NULL) { 10764543Smarks nvlist_free(who_nvp); 10774787Sahrens if (perms_nvp) 10784787Sahrens nvlist_free(perms_nvp); 10794543Smarks if (sets_nvp) 10804543Smarks nvlist_free(sets_nvp); 10814543Smarks (void) snprintf(errbuf, sizeof (errbuf), 10824543Smarks dgettext(TEXT_DOMAIN, "Who string is NULL"), 10834543Smarks whostr); 10844543Smarks return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf)); 10854543Smarks } 10864543Smarks } 10874543Smarks 10884543Smarks /* 10894543Smarks * Now create the nvlist(s) 10904543Smarks */ 10914543Smarks do { 10924543Smarks uint64_t who_id; 10934543Smarks 10944543Smarks error = zfs_get_perm_who(who_tok, &who_type, 10954543Smarks &who_id); 10964543Smarks if (error) { 10974543Smarks nvlist_free(who_nvp); 10984787Sahrens if (perms_nvp) 10994787Sahrens nvlist_free(perms_nvp); 11004543Smarks if (sets_nvp) 11014543Smarks nvlist_free(sets_nvp); 11024543Smarks (void) snprintf(errbuf, sizeof (errbuf), 11034543Smarks dgettext(TEXT_DOMAIN, 11044543Smarks "Unable to determine uid/gid for " 11054543Smarks "%s "), who_tok); 11064543Smarks return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf)); 11074543Smarks } 11084543Smarks 11094543Smarks /* 11104543Smarks * add entries for both local and descendent when required 11114543Smarks */ 11124543Smarks zfs_perms_add_who_nvlist(who_nvp, who_id, who_tok, 11134543Smarks perms_nvp, sets_nvp, who_type, inherit); 11144543Smarks 11154543Smarks } while (who_tok = strtok(NULL, ",")); 11164543Smarks *nvp = who_nvp; 11174543Smarks return (0); 11184543Smarks } 11194543Smarks 11204543Smarks static int 11214543Smarks zfs_perm_set_common(zfs_handle_t *zhp, nvlist_t *nvp, boolean_t unset) 11224543Smarks { 11234543Smarks zfs_cmd_t zc = { 0 }; 11244543Smarks int error; 11254543Smarks char errbuf[1024]; 11264543Smarks 11274543Smarks (void) snprintf(errbuf, sizeof (errbuf), 11284543Smarks dgettext(TEXT_DOMAIN, "Cannot update 'allows' for '%s'"), 11294543Smarks zhp->zfs_name); 11304543Smarks 11315094Slling if (zcmd_write_src_nvlist(zhp->zfs_hdl, &zc, nvp)) 11324543Smarks return (-1); 11334543Smarks 11344543Smarks (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 11354543Smarks zc.zc_perm_action = unset; 11364543Smarks 11374543Smarks error = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_FSACL, &zc); 11384543Smarks if (error && errno == ENOTSUP) { 11394543Smarks (void) snprintf(errbuf, sizeof (errbuf), 11404543Smarks gettext("Pool must be upgraded to use 'allow/unallow'")); 11414543Smarks zcmd_free_nvlists(&zc); 11424543Smarks return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, errbuf)); 11434543Smarks } else if (error) { 11444543Smarks return (zfs_standard_error(zhp->zfs_hdl, errno, errbuf)); 11454543Smarks } 11464543Smarks zcmd_free_nvlists(&zc); 11474543Smarks 11484543Smarks return (error); 11494543Smarks } 11504543Smarks 11514543Smarks int 11524543Smarks zfs_perm_set(zfs_handle_t *zhp, nvlist_t *nvp) 11534543Smarks { 11544543Smarks return (zfs_perm_set_common(zhp, nvp, B_FALSE)); 11554543Smarks } 11564543Smarks 11574543Smarks int 11584543Smarks zfs_perm_remove(zfs_handle_t *zhp, nvlist_t *perms) 11594543Smarks { 11604543Smarks return (zfs_perm_set_common(zhp, perms, B_TRUE)); 11614543Smarks } 11624543Smarks 11634543Smarks static int 11644543Smarks perm_compare(const void *arg1, const void *arg2) 11654543Smarks { 11664543Smarks const zfs_perm_node_t *node1 = arg1; 11674543Smarks const zfs_perm_node_t *node2 = arg2; 11684543Smarks int ret; 11694543Smarks 11704543Smarks ret = strcmp(node1->z_pname, node2->z_pname); 11714543Smarks 11724543Smarks if (ret > 0) 11734543Smarks return (1); 11744543Smarks if (ret < 0) 11754543Smarks return (-1); 11764543Smarks else 11774543Smarks return (0); 11784543Smarks } 11794543Smarks 11804543Smarks static void 11814543Smarks zfs_destroy_perm_tree(avl_tree_t *tree) 11824543Smarks { 11834543Smarks zfs_perm_node_t *permnode; 11844543Smarks void *cookie; 11854543Smarks 11864543Smarks cookie = NULL; 11874543Smarks while ((permnode = avl_destroy_nodes(tree, &cookie)) != NULL) { 11884543Smarks avl_remove(tree, permnode); 11894543Smarks free(permnode); 11904543Smarks } 11914543Smarks } 11924543Smarks 11934543Smarks static void 11944543Smarks zfs_destroy_tree(avl_tree_t *tree) 11954543Smarks { 11964543Smarks zfs_allow_node_t *allownode; 11974543Smarks void *cookie; 11984543Smarks 11994543Smarks cookie = NULL; 12004543Smarks while ((allownode = avl_destroy_nodes(tree, &cookie)) != NULL) { 12014543Smarks zfs_destroy_perm_tree(&allownode->z_localdescend); 12024543Smarks zfs_destroy_perm_tree(&allownode->z_local); 12034543Smarks zfs_destroy_perm_tree(&allownode->z_descend); 12044543Smarks avl_remove(tree, allownode); 12054543Smarks free(allownode); 12064543Smarks } 12074543Smarks } 12084543Smarks 12094543Smarks void 12104543Smarks zfs_free_allows(zfs_allow_t *allow) 12114543Smarks { 12124543Smarks zfs_allow_t *allownext; 12134543Smarks zfs_allow_t *freeallow; 12144543Smarks 12154543Smarks allownext = allow; 12164543Smarks while (allownext) { 12174543Smarks zfs_destroy_tree(&allownext->z_sets); 12184543Smarks zfs_destroy_tree(&allownext->z_crperms); 12194543Smarks zfs_destroy_tree(&allownext->z_user); 12204543Smarks zfs_destroy_tree(&allownext->z_group); 12214543Smarks zfs_destroy_tree(&allownext->z_everyone); 12224543Smarks freeallow = allownext; 12234543Smarks allownext = allownext->z_next; 12244543Smarks free(freeallow); 12254543Smarks } 12264543Smarks } 12274543Smarks 12284543Smarks static zfs_allow_t * 12294543Smarks zfs_alloc_perm_tree(zfs_handle_t *zhp, zfs_allow_t *prev, char *setpoint) 12304543Smarks { 12314543Smarks zfs_allow_t *ptree; 12324543Smarks 12334543Smarks if ((ptree = zfs_alloc(zhp->zfs_hdl, 12344543Smarks sizeof (zfs_allow_t))) == NULL) { 12354543Smarks return (NULL); 12364543Smarks } 12374543Smarks 12384543Smarks (void) strlcpy(ptree->z_setpoint, setpoint, sizeof (ptree->z_setpoint)); 12394543Smarks avl_create(&ptree->z_sets, 12404543Smarks perm_compare, sizeof (zfs_allow_node_t), 12414543Smarks offsetof(zfs_allow_node_t, z_node)); 12424543Smarks avl_create(&ptree->z_crperms, 12434543Smarks perm_compare, sizeof (zfs_allow_node_t), 12444543Smarks offsetof(zfs_allow_node_t, z_node)); 12454543Smarks avl_create(&ptree->z_user, 12464543Smarks perm_compare, sizeof (zfs_allow_node_t), 12474543Smarks offsetof(zfs_allow_node_t, z_node)); 12484543Smarks avl_create(&ptree->z_group, 12494543Smarks perm_compare, sizeof (zfs_allow_node_t), 12504543Smarks offsetof(zfs_allow_node_t, z_node)); 12514543Smarks avl_create(&ptree->z_everyone, 12524543Smarks perm_compare, sizeof (zfs_allow_node_t), 12534543Smarks offsetof(zfs_allow_node_t, z_node)); 12544543Smarks 12554543Smarks if (prev) 12564543Smarks prev->z_next = ptree; 12574543Smarks ptree->z_next = NULL; 12584543Smarks return (ptree); 12594543Smarks } 12604543Smarks 12614543Smarks /* 12624543Smarks * Add permissions to the appropriate AVL permission tree. 12634543Smarks * The appropriate tree may not be the requested tree. 12644543Smarks * For example if ld indicates a local permission, but 12654543Smarks * same permission also exists as a descendent permission 12664543Smarks * then the permission will be removed from the descendent 12674543Smarks * tree and add the the local+descendent tree. 12684543Smarks */ 12694543Smarks static int 12704543Smarks zfs_coalesce_perm(zfs_handle_t *zhp, zfs_allow_node_t *allownode, 12714543Smarks char *perm, char ld) 12724543Smarks { 12734543Smarks zfs_perm_node_t pnode, *permnode, *permnode2; 12744543Smarks zfs_perm_node_t *newnode; 12754543Smarks avl_index_t where, where2; 12764543Smarks avl_tree_t *tree, *altree; 12774543Smarks 12784543Smarks (void) strlcpy(pnode.z_pname, perm, sizeof (pnode.z_pname)); 12794543Smarks 12804543Smarks if (ld == ZFS_DELEG_NA) { 12814543Smarks tree = &allownode->z_localdescend; 12824543Smarks altree = &allownode->z_descend; 12834543Smarks } else if (ld == ZFS_DELEG_LOCAL) { 12844543Smarks tree = &allownode->z_local; 12854543Smarks altree = &allownode->z_descend; 12864543Smarks } else { 12874543Smarks tree = &allownode->z_descend; 12884543Smarks altree = &allownode->z_local; 12894543Smarks } 12904543Smarks permnode = avl_find(tree, &pnode, &where); 12914543Smarks permnode2 = avl_find(altree, &pnode, &where2); 12924543Smarks 12934543Smarks if (permnode2) { 12944543Smarks avl_remove(altree, permnode2); 12954543Smarks free(permnode2); 12964543Smarks if (permnode == NULL) { 12974543Smarks tree = &allownode->z_localdescend; 12984543Smarks } 12994543Smarks } 13004543Smarks 13014543Smarks /* 13024543Smarks * Now insert new permission in either requested location 13034543Smarks * local/descendent or into ld when perm will exist in both. 13044543Smarks */ 13054543Smarks if (permnode == NULL) { 13064543Smarks if ((newnode = zfs_alloc(zhp->zfs_hdl, 13074543Smarks sizeof (zfs_perm_node_t))) == NULL) { 13084543Smarks return (-1); 13094543Smarks } 13104543Smarks *newnode = pnode; 13114543Smarks avl_add(tree, newnode); 13124543Smarks } 13134543Smarks return (0); 13144543Smarks } 13154577Sahrens 13164543Smarks /* 13174543Smarks * Uggh, this is going to be a bit complicated. 13184543Smarks * we have an nvlist coming out of the kernel that 13194543Smarks * will indicate where the permission is set and then 13204543Smarks * it will contain allow of the various "who's", and what 13214543Smarks * their permissions are. To further complicate this 13224543Smarks * we will then have to coalesce the local,descendent 13234543Smarks * and local+descendent permissions where appropriate. 13244543Smarks * The kernel only knows about a permission as being local 13254543Smarks * or descendent, but not both. 13264543Smarks * 13274543Smarks * In order to make this easier for zfs_main to deal with 13284543Smarks * a series of AVL trees will be used to maintain 13294543Smarks * all of this, primarily for sorting purposes as well 13304543Smarks * as the ability to quickly locate a specific entry. 13314543Smarks * 13324543Smarks * What we end up with are tree's for sets, create perms, 13334543Smarks * user, groups and everyone. With each of those trees 13344543Smarks * we have subtrees for local, descendent and local+descendent 13354543Smarks * permissions. 13364543Smarks */ 13374543Smarks int 13384543Smarks zfs_perm_get(zfs_handle_t *zhp, zfs_allow_t **zfs_perms) 13394543Smarks { 13404543Smarks zfs_cmd_t zc = { 0 }; 13414543Smarks int error; 13424543Smarks nvlist_t *nvlist; 13434543Smarks nvlist_t *permnv, *sourcenv; 13444543Smarks nvpair_t *who_pair, *source_pair; 13454543Smarks nvpair_t *perm_pair; 13464543Smarks char errbuf[1024]; 13474543Smarks zfs_allow_t *zallowp, *newallowp; 13484543Smarks char ld; 13494543Smarks char *nvpname; 13504543Smarks uid_t uid; 13514543Smarks gid_t gid; 13524543Smarks avl_tree_t *tree; 13534543Smarks avl_index_t where; 13544543Smarks 13554543Smarks (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 13564543Smarks 13574543Smarks if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 13584543Smarks return (-1); 13594543Smarks 13604543Smarks while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) { 13614543Smarks if (errno == ENOMEM) { 13624543Smarks if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, &zc) != 0) { 13634543Smarks zcmd_free_nvlists(&zc); 13644543Smarks return (-1); 13654543Smarks } 13664543Smarks } else if (errno == ENOTSUP) { 13674543Smarks zcmd_free_nvlists(&zc); 13684543Smarks (void) snprintf(errbuf, sizeof (errbuf), 13694543Smarks gettext("Pool must be upgraded to use 'allow'")); 13704543Smarks return (zfs_error(zhp->zfs_hdl, 13714543Smarks EZFS_BADVERSION, errbuf)); 13724543Smarks } else { 13734543Smarks zcmd_free_nvlists(&zc); 13744543Smarks return (-1); 13754543Smarks } 13764543Smarks } 13774543Smarks 13784543Smarks if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &nvlist) != 0) { 13794543Smarks zcmd_free_nvlists(&zc); 13804543Smarks return (-1); 13814543Smarks } 13824543Smarks 13834543Smarks zcmd_free_nvlists(&zc); 13844543Smarks 13854543Smarks source_pair = nvlist_next_nvpair(nvlist, NULL); 13864543Smarks 13874543Smarks if (source_pair == NULL) { 13884543Smarks *zfs_perms = NULL; 13894543Smarks return (0); 13904543Smarks } 13914543Smarks 13924543Smarks *zfs_perms = zfs_alloc_perm_tree(zhp, NULL, nvpair_name(source_pair)); 13934543Smarks if (*zfs_perms == NULL) { 13944543Smarks return (0); 13954543Smarks } 13964543Smarks 13974543Smarks zallowp = *zfs_perms; 13984543Smarks 13994543Smarks for (;;) { 14004543Smarks struct passwd *pwd; 14014543Smarks struct group *grp; 14024543Smarks zfs_allow_node_t *allownode; 14034543Smarks zfs_allow_node_t findallownode; 14044543Smarks zfs_allow_node_t *newallownode; 14054543Smarks 14064543Smarks (void) strlcpy(zallowp->z_setpoint, 14074543Smarks nvpair_name(source_pair), 14084543Smarks sizeof (zallowp->z_setpoint)); 14094543Smarks 14104543Smarks if ((error = nvpair_value_nvlist(source_pair, &sourcenv)) != 0) 14114543Smarks goto abort; 14124543Smarks 14134543Smarks /* 14144543Smarks * Make sure nvlist is composed correctly 14154543Smarks */ 14164543Smarks if (zfs_deleg_verify_nvlist(sourcenv)) { 14174543Smarks goto abort; 14184543Smarks } 14194543Smarks 14204543Smarks who_pair = nvlist_next_nvpair(sourcenv, NULL); 14214543Smarks if (who_pair == NULL) { 14224543Smarks goto abort; 14234543Smarks } 14244543Smarks 14254543Smarks do { 14264543Smarks error = nvpair_value_nvlist(who_pair, &permnv); 14274543Smarks if (error) { 14284543Smarks goto abort; 14294543Smarks } 14304543Smarks 14314543Smarks /* 14324543Smarks * First build up the key to use 14334543Smarks * for looking up in the various 14344543Smarks * who trees. 14354543Smarks */ 14364543Smarks ld = nvpair_name(who_pair)[1]; 14374543Smarks nvpname = nvpair_name(who_pair); 14384543Smarks switch (nvpair_name(who_pair)[0]) { 14394543Smarks case ZFS_DELEG_USER: 14404543Smarks case ZFS_DELEG_USER_SETS: 14414543Smarks tree = &zallowp->z_user; 14424543Smarks uid = atol(&nvpname[3]); 14434543Smarks pwd = getpwuid(uid); 14444543Smarks (void) snprintf(findallownode.z_key, 14454543Smarks sizeof (findallownode.z_key), "user %s", 14464543Smarks (pwd) ? pwd->pw_name : 14474543Smarks &nvpair_name(who_pair)[3]); 14484543Smarks break; 14494543Smarks case ZFS_DELEG_GROUP: 14504543Smarks case ZFS_DELEG_GROUP_SETS: 14514543Smarks tree = &zallowp->z_group; 14524543Smarks gid = atol(&nvpname[3]); 14534543Smarks grp = getgrgid(gid); 14544543Smarks (void) snprintf(findallownode.z_key, 14554543Smarks sizeof (findallownode.z_key), "group %s", 14564543Smarks (grp) ? grp->gr_name : 14574543Smarks &nvpair_name(who_pair)[3]); 14584543Smarks break; 14594543Smarks case ZFS_DELEG_CREATE: 14604543Smarks case ZFS_DELEG_CREATE_SETS: 14614543Smarks tree = &zallowp->z_crperms; 14624543Smarks (void) strlcpy(findallownode.z_key, "", 14634543Smarks sizeof (findallownode.z_key)); 14644543Smarks break; 14654543Smarks case ZFS_DELEG_EVERYONE: 14664543Smarks case ZFS_DELEG_EVERYONE_SETS: 14674543Smarks (void) snprintf(findallownode.z_key, 14684543Smarks sizeof (findallownode.z_key), "everyone"); 14694543Smarks tree = &zallowp->z_everyone; 14704543Smarks break; 14714543Smarks case ZFS_DELEG_NAMED_SET: 14724543Smarks case ZFS_DELEG_NAMED_SET_SETS: 14734543Smarks (void) snprintf(findallownode.z_key, 14744543Smarks sizeof (findallownode.z_key), "%s", 14754543Smarks &nvpair_name(who_pair)[3]); 14764543Smarks tree = &zallowp->z_sets; 14774543Smarks break; 14784543Smarks } 14794543Smarks 14804543Smarks /* 14814543Smarks * Place who in tree 14824543Smarks */ 14834543Smarks allownode = avl_find(tree, &findallownode, &where); 14844543Smarks if (allownode == NULL) { 14854543Smarks if ((newallownode = zfs_alloc(zhp->zfs_hdl, 14864543Smarks sizeof (zfs_allow_node_t))) == NULL) { 14874543Smarks goto abort; 14884543Smarks } 14894543Smarks avl_create(&newallownode->z_localdescend, 14904543Smarks perm_compare, 14914543Smarks sizeof (zfs_perm_node_t), 14924543Smarks offsetof(zfs_perm_node_t, z_node)); 14934543Smarks avl_create(&newallownode->z_local, 14944543Smarks perm_compare, 14954543Smarks sizeof (zfs_perm_node_t), 14964543Smarks offsetof(zfs_perm_node_t, z_node)); 14974543Smarks avl_create(&newallownode->z_descend, 14984543Smarks perm_compare, 14994543Smarks sizeof (zfs_perm_node_t), 15004543Smarks offsetof(zfs_perm_node_t, z_node)); 15014543Smarks (void) strlcpy(newallownode->z_key, 15024543Smarks findallownode.z_key, 15034543Smarks sizeof (findallownode.z_key)); 15044543Smarks avl_insert(tree, newallownode, where); 15054543Smarks allownode = newallownode; 15064543Smarks } 15074543Smarks 15084543Smarks /* 15094543Smarks * Now iterate over the permissions and 15104543Smarks * place them in the appropriate local, 15114543Smarks * descendent or local+descendent tree. 15124543Smarks * 15134543Smarks * The permissions are added to the tree 15144543Smarks * via zfs_coalesce_perm(). 15154543Smarks */ 15164543Smarks perm_pair = nvlist_next_nvpair(permnv, NULL); 15174543Smarks if (perm_pair == NULL) 15184543Smarks goto abort; 15194543Smarks do { 15204543Smarks if (zfs_coalesce_perm(zhp, allownode, 15214543Smarks nvpair_name(perm_pair), ld) != 0) 15224543Smarks goto abort; 15234543Smarks } while (perm_pair = nvlist_next_nvpair(permnv, 15244543Smarks perm_pair)); 15254543Smarks } while (who_pair = nvlist_next_nvpair(sourcenv, who_pair)); 15264543Smarks 15274543Smarks source_pair = nvlist_next_nvpair(nvlist, source_pair); 15284543Smarks if (source_pair == NULL) 15294543Smarks break; 15304543Smarks 15314543Smarks /* 15324543Smarks * allocate another node from the link list of 15334543Smarks * zfs_allow_t structures 15344543Smarks */ 15354543Smarks newallowp = zfs_alloc_perm_tree(zhp, zallowp, 15364543Smarks nvpair_name(source_pair)); 15374543Smarks if (newallowp == NULL) { 15384543Smarks goto abort; 15394543Smarks } 15404543Smarks zallowp = newallowp; 15414543Smarks } 15424543Smarks nvlist_free(nvlist); 15434543Smarks return (0); 15444543Smarks abort: 15454543Smarks zfs_free_allows(*zfs_perms); 15464543Smarks nvlist_free(nvlist); 15474543Smarks return (-1); 15484543Smarks } 15494543Smarks 1550789Sahrens /* 1551789Sahrens * Given a property name and value, set the property for the given dataset. 1552789Sahrens */ 1553789Sahrens int 15542676Seschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval) 1555789Sahrens { 1556789Sahrens zfs_cmd_t zc = { 0 }; 15572676Seschrock int ret = -1; 15582676Seschrock prop_changelist_t *cl = NULL; 15592082Seschrock char errbuf[1024]; 15602082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 15612676Seschrock nvlist_t *nvl = NULL, *realprops; 15622676Seschrock zfs_prop_t prop; 15632082Seschrock 15642082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 15652676Seschrock dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 15662082Seschrock zhp->zfs_name); 15672082Seschrock 15682676Seschrock if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 || 15692676Seschrock nvlist_add_string(nvl, propname, propval) != 0) { 15702676Seschrock (void) no_memory(hdl); 15712676Seschrock goto error; 1572789Sahrens } 1573789Sahrens 15745094Slling if ((realprops = zfs_validate_properties(hdl, zhp->zfs_type, nvl, 15752676Seschrock zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL) 15762676Seschrock goto error; 15775094Slling 15782676Seschrock nvlist_free(nvl); 15792676Seschrock nvl = realprops; 15802676Seschrock 15812676Seschrock prop = zfs_name_to_prop(propname); 15822676Seschrock 1583789Sahrens if ((cl = changelist_gather(zhp, prop, 0)) == NULL) 15842676Seschrock goto error; 1585789Sahrens 1586789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 15872082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 15882082Seschrock "child dataset with inherited mountpoint is used " 15892082Seschrock "in a non-global zone")); 15902082Seschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1591789Sahrens goto error; 1592789Sahrens } 1593789Sahrens 1594789Sahrens if ((ret = changelist_prefix(cl)) != 0) 1595789Sahrens goto error; 1596789Sahrens 1597789Sahrens /* 1598789Sahrens * Execute the corresponding ioctl() to set this property. 1599789Sahrens */ 1600789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1601789Sahrens 16025094Slling if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0) 16032676Seschrock goto error; 16042676Seschrock 16054543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc); 1606789Sahrens 1607789Sahrens if (ret != 0) { 1608789Sahrens switch (errno) { 1609789Sahrens 1610789Sahrens case ENOSPC: 1611789Sahrens /* 1612789Sahrens * For quotas and reservations, ENOSPC indicates 1613789Sahrens * something different; setting a quota or reservation 1614789Sahrens * doesn't use any disk space. 1615789Sahrens */ 1616789Sahrens switch (prop) { 1617789Sahrens case ZFS_PROP_QUOTA: 16182082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 16192082Seschrock "size is less than current used or " 16202082Seschrock "reserved space")); 16212082Seschrock (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 1622789Sahrens break; 1623789Sahrens 1624789Sahrens case ZFS_PROP_RESERVATION: 16252082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 16262082Seschrock "size is greater than available space")); 16272082Seschrock (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 1628789Sahrens break; 1629789Sahrens 1630789Sahrens default: 16312082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 1632789Sahrens break; 1633789Sahrens } 1634789Sahrens break; 1635789Sahrens 1636789Sahrens case EBUSY: 16372082Seschrock if (prop == ZFS_PROP_VOLBLOCKSIZE) 16382082Seschrock (void) zfs_error(hdl, EZFS_VOLHASDATA, errbuf); 16392082Seschrock else 16402676Seschrock (void) zfs_standard_error(hdl, EBUSY, errbuf); 1641789Sahrens break; 1642789Sahrens 16431175Slling case EROFS: 16442082Seschrock (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf); 16451175Slling break; 16461175Slling 16473886Sahl case ENOTSUP: 16483886Sahl zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 16494603Sahrens "pool must be upgraded to set this " 16504603Sahrens "property or value")); 16513886Sahl (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 16523886Sahl break; 16533886Sahl 1654789Sahrens case EOVERFLOW: 1655789Sahrens /* 1656789Sahrens * This platform can't address a volume this big. 1657789Sahrens */ 1658789Sahrens #ifdef _ILP32 1659789Sahrens if (prop == ZFS_PROP_VOLSIZE) { 16602082Seschrock (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf); 1661789Sahrens break; 1662789Sahrens } 1663789Sahrens #endif 16642082Seschrock /* FALLTHROUGH */ 1665789Sahrens default: 16662082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 1667789Sahrens } 1668789Sahrens } else { 1669789Sahrens /* 1670789Sahrens * Refresh the statistics so the new property value 1671789Sahrens * is reflected. 1672789Sahrens */ 16732676Seschrock if ((ret = changelist_postfix(cl)) == 0) 16742676Seschrock (void) get_stats(zhp); 1675789Sahrens } 1676789Sahrens 1677789Sahrens error: 16782676Seschrock nvlist_free(nvl); 16792676Seschrock zcmd_free_nvlists(&zc); 16802676Seschrock if (cl) 16812676Seschrock changelist_free(cl); 1682789Sahrens return (ret); 1683789Sahrens } 1684789Sahrens 1685789Sahrens /* 1686789Sahrens * Given a property, inherit the value from the parent dataset. 1687789Sahrens */ 1688789Sahrens int 16892676Seschrock zfs_prop_inherit(zfs_handle_t *zhp, const char *propname) 1690789Sahrens { 1691789Sahrens zfs_cmd_t zc = { 0 }; 1692789Sahrens int ret; 1693789Sahrens prop_changelist_t *cl; 16942082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 16952082Seschrock char errbuf[1024]; 16962676Seschrock zfs_prop_t prop; 16972082Seschrock 16982082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 16992082Seschrock "cannot inherit %s for '%s'"), propname, zhp->zfs_name); 1700789Sahrens 17015094Slling if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) { 17022676Seschrock /* 17032676Seschrock * For user properties, the amount of work we have to do is very 17042676Seschrock * small, so just do it here. 17052676Seschrock */ 17062676Seschrock if (!zfs_prop_user(propname)) { 17072676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 17082676Seschrock "invalid property")); 17092676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 17102676Seschrock } 17112676Seschrock 17122676Seschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 17132676Seschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 17142676Seschrock 17154849Sahrens if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0) 17162676Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 17172676Seschrock 17182676Seschrock return (0); 17192676Seschrock } 17202676Seschrock 1721789Sahrens /* 1722789Sahrens * Verify that this property is inheritable. 1723789Sahrens */ 17242082Seschrock if (zfs_prop_readonly(prop)) 17252082Seschrock return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf)); 17262082Seschrock 17272082Seschrock if (!zfs_prop_inheritable(prop)) 17282082Seschrock return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf)); 1729789Sahrens 1730789Sahrens /* 1731789Sahrens * Check to see if the value applies to this type 1732789Sahrens */ 17332082Seschrock if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 17342082Seschrock return (zfs_error(hdl, EZFS_PROPTYPE, errbuf)); 1735789Sahrens 17363443Srm160521 /* 17373443Srm160521 * Normalize the name, to get rid of shorthand abbrevations. 17383443Srm160521 */ 17393443Srm160521 propname = zfs_prop_to_name(prop); 1740789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 17412676Seschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 1742789Sahrens 1743789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID && 1744789Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 17452082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 17462082Seschrock "dataset is used in a non-global zone")); 17472082Seschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 1748789Sahrens } 1749789Sahrens 1750789Sahrens /* 1751789Sahrens * Determine datasets which will be affected by this change, if any. 1752789Sahrens */ 1753789Sahrens if ((cl = changelist_gather(zhp, prop, 0)) == NULL) 1754789Sahrens return (-1); 1755789Sahrens 1756789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 17572082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 17582082Seschrock "child dataset with inherited mountpoint is used " 17592082Seschrock "in a non-global zone")); 17602082Seschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1761789Sahrens goto error; 1762789Sahrens } 1763789Sahrens 1764789Sahrens if ((ret = changelist_prefix(cl)) != 0) 1765789Sahrens goto error; 1766789Sahrens 17674849Sahrens if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) { 17682082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 1769789Sahrens } else { 1770789Sahrens 17712169Snd150628 if ((ret = changelist_postfix(cl)) != 0) 1772789Sahrens goto error; 1773789Sahrens 1774789Sahrens /* 1775789Sahrens * Refresh the statistics so the new property is reflected. 1776789Sahrens */ 1777789Sahrens (void) get_stats(zhp); 1778789Sahrens } 1779789Sahrens 1780789Sahrens error: 1781789Sahrens changelist_free(cl); 1782789Sahrens return (ret); 1783789Sahrens } 1784789Sahrens 1785789Sahrens /* 17861356Seschrock * True DSL properties are stored in an nvlist. The following two functions 17871356Seschrock * extract them appropriately. 17881356Seschrock */ 17891356Seschrock static uint64_t 17901356Seschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 17911356Seschrock { 17921356Seschrock nvlist_t *nv; 17931356Seschrock uint64_t value; 17941356Seschrock 17952885Sahrens *source = NULL; 17961356Seschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 17971356Seschrock zfs_prop_to_name(prop), &nv) == 0) { 17985094Slling verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0); 17995094Slling (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 18001356Seschrock } else { 18011356Seschrock value = zfs_prop_default_numeric(prop); 18021356Seschrock *source = ""; 18031356Seschrock } 18041356Seschrock 18051356Seschrock return (value); 18061356Seschrock } 18071356Seschrock 18081356Seschrock static char * 18091356Seschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 18101356Seschrock { 18111356Seschrock nvlist_t *nv; 18121356Seschrock char *value; 18131356Seschrock 18142885Sahrens *source = NULL; 18151356Seschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 18161356Seschrock zfs_prop_to_name(prop), &nv) == 0) { 18175094Slling verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0); 18185094Slling (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 18191356Seschrock } else { 18201356Seschrock if ((value = (char *)zfs_prop_default_string(prop)) == NULL) 18211356Seschrock value = ""; 18221356Seschrock *source = ""; 18231356Seschrock } 18241356Seschrock 18251356Seschrock return (value); 18261356Seschrock } 18271356Seschrock 18281356Seschrock /* 1829789Sahrens * Internal function for getting a numeric property. Both zfs_prop_get() and 1830789Sahrens * zfs_prop_get_int() are built using this interface. 1831789Sahrens * 1832789Sahrens * Certain properties can be overridden using 'mount -o'. In this case, scan 1833789Sahrens * the contents of the /etc/mnttab entry, searching for the appropriate options. 1834789Sahrens * If they differ from the on-disk values, report the current values and mark 1835789Sahrens * the source "temporary". 1836789Sahrens */ 18372082Seschrock static int 18385094Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src, 18392082Seschrock char **source, uint64_t *val) 1840789Sahrens { 18415147Srm160521 zfs_cmd_t zc = { 0 }; 1842789Sahrens struct mnttab mnt; 18433265Sahrens char *mntopt_on = NULL; 18443265Sahrens char *mntopt_off = NULL; 1845789Sahrens 1846789Sahrens *source = NULL; 1847789Sahrens 18483265Sahrens switch (prop) { 18493265Sahrens case ZFS_PROP_ATIME: 18503265Sahrens mntopt_on = MNTOPT_ATIME; 18513265Sahrens mntopt_off = MNTOPT_NOATIME; 18523265Sahrens break; 18533265Sahrens 18543265Sahrens case ZFS_PROP_DEVICES: 18553265Sahrens mntopt_on = MNTOPT_DEVICES; 18563265Sahrens mntopt_off = MNTOPT_NODEVICES; 18573265Sahrens break; 18583265Sahrens 18593265Sahrens case ZFS_PROP_EXEC: 18603265Sahrens mntopt_on = MNTOPT_EXEC; 18613265Sahrens mntopt_off = MNTOPT_NOEXEC; 18623265Sahrens break; 18633265Sahrens 18643265Sahrens case ZFS_PROP_READONLY: 18653265Sahrens mntopt_on = MNTOPT_RO; 18663265Sahrens mntopt_off = MNTOPT_RW; 18673265Sahrens break; 18683265Sahrens 18693265Sahrens case ZFS_PROP_SETUID: 18703265Sahrens mntopt_on = MNTOPT_SETUID; 18713265Sahrens mntopt_off = MNTOPT_NOSETUID; 18723265Sahrens break; 18733265Sahrens 18743265Sahrens case ZFS_PROP_XATTR: 18753265Sahrens mntopt_on = MNTOPT_XATTR; 18763265Sahrens mntopt_off = MNTOPT_NOXATTR; 18773265Sahrens break; 1878*5331Samw 1879*5331Samw case ZFS_PROP_NBMAND: 1880*5331Samw mntopt_on = MNTOPT_NBMAND; 1881*5331Samw mntopt_off = MNTOPT_NONBMAND; 1882*5331Samw break; 18833265Sahrens } 18843265Sahrens 18852474Seschrock /* 18862474Seschrock * Because looking up the mount options is potentially expensive 18872474Seschrock * (iterating over all of /etc/mnttab), we defer its calculation until 18882474Seschrock * we're looking up a property which requires its presence. 18892474Seschrock */ 18902474Seschrock if (!zhp->zfs_mntcheck && 18913265Sahrens (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) { 18923265Sahrens struct mnttab entry, search = { 0 }; 18933265Sahrens FILE *mnttab = zhp->zfs_hdl->libzfs_mnttab; 18942474Seschrock 18952474Seschrock search.mnt_special = (char *)zhp->zfs_name; 18962474Seschrock search.mnt_fstype = MNTTYPE_ZFS; 18973265Sahrens rewind(mnttab); 18983265Sahrens 18993265Sahrens if (getmntany(mnttab, &entry, &search) == 0) { 19003265Sahrens zhp->zfs_mntopts = zfs_strdup(zhp->zfs_hdl, 19013265Sahrens entry.mnt_mntopts); 19023265Sahrens if (zhp->zfs_mntopts == NULL) 19033265Sahrens return (-1); 19043265Sahrens } 19052474Seschrock 19062474Seschrock zhp->zfs_mntcheck = B_TRUE; 19072474Seschrock } 19082474Seschrock 1909789Sahrens if (zhp->zfs_mntopts == NULL) 1910789Sahrens mnt.mnt_mntopts = ""; 1911789Sahrens else 1912789Sahrens mnt.mnt_mntopts = zhp->zfs_mntopts; 1913789Sahrens 1914789Sahrens switch (prop) { 1915789Sahrens case ZFS_PROP_ATIME: 19163265Sahrens case ZFS_PROP_DEVICES: 19173265Sahrens case ZFS_PROP_EXEC: 19183265Sahrens case ZFS_PROP_READONLY: 19193265Sahrens case ZFS_PROP_SETUID: 19203265Sahrens case ZFS_PROP_XATTR: 1921*5331Samw case ZFS_PROP_NBMAND: 19222082Seschrock *val = getprop_uint64(zhp, prop, source); 19232082Seschrock 19243265Sahrens if (hasmntopt(&mnt, mntopt_on) && !*val) { 19252082Seschrock *val = B_TRUE; 1926789Sahrens if (src) 19275094Slling *src = ZPROP_SRC_TEMPORARY; 19283265Sahrens } else if (hasmntopt(&mnt, mntopt_off) && *val) { 19292082Seschrock *val = B_FALSE; 1930789Sahrens if (src) 19315094Slling *src = ZPROP_SRC_TEMPORARY; 1932789Sahrens } 19332082Seschrock break; 1934789Sahrens 19353265Sahrens case ZFS_PROP_CANMOUNT: 19362082Seschrock *val = getprop_uint64(zhp, prop, source); 19373417Srm160521 if (*val == 0) 19383417Srm160521 *source = zhp->zfs_name; 19393417Srm160521 else 19403417Srm160521 *source = ""; /* default */ 19412082Seschrock break; 1942789Sahrens 1943789Sahrens case ZFS_PROP_QUOTA: 1944789Sahrens case ZFS_PROP_RESERVATION: 19452885Sahrens *val = getprop_uint64(zhp, prop, source); 19462885Sahrens if (*val == 0) 1947789Sahrens *source = ""; /* default */ 1948789Sahrens else 1949789Sahrens *source = zhp->zfs_name; 19502082Seschrock break; 1951789Sahrens 1952789Sahrens case ZFS_PROP_MOUNTED: 19532082Seschrock *val = (zhp->zfs_mntopts != NULL); 19542082Seschrock break; 1955789Sahrens 19563377Seschrock case ZFS_PROP_NUMCLONES: 19573377Seschrock *val = zhp->zfs_dmustats.dds_num_clones; 19583377Seschrock break; 19593377Seschrock 19605147Srm160521 case ZFS_PROP_VERSION: 19615147Srm160521 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 19625147Srm160521 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_VERSION, &zc) || 19635147Srm160521 (zc.zc_cookie == 0)) { 19645147Srm160521 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 19655147Srm160521 "unable to get version property")); 19665147Srm160521 return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, 19675147Srm160521 dgettext(TEXT_DOMAIN, "internal error"))); 19685147Srm160521 } 19695147Srm160521 *val = zc.zc_cookie; 19705147Srm160521 break; 19715147Srm160521 1972789Sahrens default: 19734577Sahrens switch (zfs_prop_get_type(prop)) { 19744787Sahrens case PROP_TYPE_NUMBER: 19754787Sahrens case PROP_TYPE_INDEX: 19764577Sahrens *val = getprop_uint64(zhp, prop, source); 19774577Sahrens break; 19784577Sahrens 19794787Sahrens case PROP_TYPE_STRING: 19804577Sahrens default: 19814577Sahrens zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 19824577Sahrens "cannot get non-numeric property")); 19834577Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP, 19844577Sahrens dgettext(TEXT_DOMAIN, "internal error"))); 19854577Sahrens } 1986789Sahrens } 1987789Sahrens 1988789Sahrens return (0); 1989789Sahrens } 1990789Sahrens 1991789Sahrens /* 1992789Sahrens * Calculate the source type, given the raw source string. 1993789Sahrens */ 1994789Sahrens static void 19955094Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source, 1996789Sahrens char *statbuf, size_t statlen) 1997789Sahrens { 19985094Slling if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY) 1999789Sahrens return; 2000789Sahrens 2001789Sahrens if (source == NULL) { 20025094Slling *srctype = ZPROP_SRC_NONE; 2003789Sahrens } else if (source[0] == '\0') { 20045094Slling *srctype = ZPROP_SRC_DEFAULT; 2005789Sahrens } else { 2006789Sahrens if (strcmp(source, zhp->zfs_name) == 0) { 20075094Slling *srctype = ZPROP_SRC_LOCAL; 2008789Sahrens } else { 2009789Sahrens (void) strlcpy(statbuf, source, statlen); 20105094Slling *srctype = ZPROP_SRC_INHERITED; 2011789Sahrens } 2012789Sahrens } 2013789Sahrens 2014789Sahrens } 2015789Sahrens 2016789Sahrens /* 2017789Sahrens * Retrieve a property from the given object. If 'literal' is specified, then 2018789Sahrens * numbers are left as exact values. Otherwise, numbers are converted to a 2019789Sahrens * human-readable form. 2020789Sahrens * 2021789Sahrens * Returns 0 on success, or -1 on error. 2022789Sahrens */ 2023789Sahrens int 2024789Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen, 20255094Slling zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal) 2026789Sahrens { 2027789Sahrens char *source = NULL; 2028789Sahrens uint64_t val; 2029789Sahrens char *str; 2030789Sahrens const char *root; 20312676Seschrock const char *strval; 2032789Sahrens 2033789Sahrens /* 2034789Sahrens * Check to see if this property applies to our object 2035789Sahrens */ 2036789Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 2037789Sahrens return (-1); 2038789Sahrens 2039789Sahrens if (src) 20405094Slling *src = ZPROP_SRC_NONE; 2041789Sahrens 2042789Sahrens switch (prop) { 2043789Sahrens case ZFS_PROP_CREATION: 2044789Sahrens /* 2045789Sahrens * 'creation' is a time_t stored in the statistics. We convert 2046789Sahrens * this into a string unless 'literal' is specified. 2047789Sahrens */ 2048789Sahrens { 20492885Sahrens val = getprop_uint64(zhp, prop, &source); 20502885Sahrens time_t time = (time_t)val; 2051789Sahrens struct tm t; 2052789Sahrens 2053789Sahrens if (literal || 2054789Sahrens localtime_r(&time, &t) == NULL || 2055789Sahrens strftime(propbuf, proplen, "%a %b %e %k:%M %Y", 2056789Sahrens &t) == 0) 20572885Sahrens (void) snprintf(propbuf, proplen, "%llu", val); 2058789Sahrens } 2059789Sahrens break; 2060789Sahrens 2061789Sahrens case ZFS_PROP_MOUNTPOINT: 2062789Sahrens /* 2063789Sahrens * Getting the precise mountpoint can be tricky. 2064789Sahrens * 2065789Sahrens * - for 'none' or 'legacy', return those values. 2066789Sahrens * - for default mountpoints, construct it as /zfs/<dataset> 2067789Sahrens * - for inherited mountpoints, we want to take everything 2068789Sahrens * after our ancestor and append it to the inherited value. 2069789Sahrens * 2070789Sahrens * If the pool has an alternate root, we want to prepend that 2071789Sahrens * root to any values we return. 2072789Sahrens */ 20731544Seschrock root = zhp->zfs_root; 20741356Seschrock str = getprop_string(zhp, prop, &source); 20751356Seschrock 20761356Seschrock if (str[0] == '\0') { 2077789Sahrens (void) snprintf(propbuf, proplen, "%s/zfs/%s", 2078789Sahrens root, zhp->zfs_name); 20791356Seschrock } else if (str[0] == '/') { 20801356Seschrock const char *relpath = zhp->zfs_name + strlen(source); 2081789Sahrens 2082789Sahrens if (relpath[0] == '/') 2083789Sahrens relpath++; 20841356Seschrock if (str[1] == '\0') 20851356Seschrock str++; 2086789Sahrens 2087789Sahrens if (relpath[0] == '\0') 2088789Sahrens (void) snprintf(propbuf, proplen, "%s%s", 20891356Seschrock root, str); 2090789Sahrens else 2091789Sahrens (void) snprintf(propbuf, proplen, "%s%s%s%s", 20921356Seschrock root, str, relpath[0] == '@' ? "" : "/", 2093789Sahrens relpath); 2094789Sahrens } else { 2095789Sahrens /* 'legacy' or 'none' */ 20961356Seschrock (void) strlcpy(propbuf, str, proplen); 2097789Sahrens } 2098789Sahrens 2099789Sahrens break; 2100789Sahrens 2101789Sahrens case ZFS_PROP_ORIGIN: 21022885Sahrens (void) strlcpy(propbuf, getprop_string(zhp, prop, &source), 2103789Sahrens proplen); 2104789Sahrens /* 2105789Sahrens * If there is no parent at all, return failure to indicate that 2106789Sahrens * it doesn't apply to this dataset. 2107789Sahrens */ 2108789Sahrens if (propbuf[0] == '\0') 2109789Sahrens return (-1); 2110789Sahrens break; 2111789Sahrens 2112789Sahrens case ZFS_PROP_QUOTA: 2113789Sahrens case ZFS_PROP_RESERVATION: 21142082Seschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 21152082Seschrock return (-1); 2116789Sahrens 2117789Sahrens /* 2118789Sahrens * If quota or reservation is 0, we translate this into 'none' 2119789Sahrens * (unless literal is set), and indicate that it's the default 2120789Sahrens * value. Otherwise, we print the number nicely and indicate 2121789Sahrens * that its set locally. 2122789Sahrens */ 2123789Sahrens if (val == 0) { 2124789Sahrens if (literal) 2125789Sahrens (void) strlcpy(propbuf, "0", proplen); 2126789Sahrens else 2127789Sahrens (void) strlcpy(propbuf, "none", proplen); 2128789Sahrens } else { 2129789Sahrens if (literal) 21302856Snd150628 (void) snprintf(propbuf, proplen, "%llu", 21313912Slling (u_longlong_t)val); 2132789Sahrens else 2133789Sahrens zfs_nicenum(val, propbuf, proplen); 2134789Sahrens } 2135789Sahrens break; 2136789Sahrens 2137789Sahrens case ZFS_PROP_COMPRESSRATIO: 21382082Seschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 21392082Seschrock return (-1); 21402856Snd150628 (void) snprintf(propbuf, proplen, "%lld.%02lldx", (longlong_t) 21412856Snd150628 val / 100, (longlong_t)val % 100); 2142789Sahrens break; 2143789Sahrens 2144789Sahrens case ZFS_PROP_TYPE: 2145789Sahrens switch (zhp->zfs_type) { 2146789Sahrens case ZFS_TYPE_FILESYSTEM: 2147789Sahrens str = "filesystem"; 2148789Sahrens break; 2149789Sahrens case ZFS_TYPE_VOLUME: 2150789Sahrens str = "volume"; 2151789Sahrens break; 2152789Sahrens case ZFS_TYPE_SNAPSHOT: 2153789Sahrens str = "snapshot"; 2154789Sahrens break; 2155789Sahrens default: 21562082Seschrock abort(); 2157789Sahrens } 2158789Sahrens (void) snprintf(propbuf, proplen, "%s", str); 2159789Sahrens break; 2160789Sahrens 2161789Sahrens case ZFS_PROP_MOUNTED: 2162789Sahrens /* 2163789Sahrens * The 'mounted' property is a pseudo-property that described 2164789Sahrens * whether the filesystem is currently mounted. Even though 2165789Sahrens * it's a boolean value, the typical values of "on" and "off" 2166789Sahrens * don't make sense, so we translate to "yes" and "no". 2167789Sahrens */ 21682082Seschrock if (get_numeric_property(zhp, ZFS_PROP_MOUNTED, 21692082Seschrock src, &source, &val) != 0) 21702082Seschrock return (-1); 21712082Seschrock if (val) 2172789Sahrens (void) strlcpy(propbuf, "yes", proplen); 2173789Sahrens else 2174789Sahrens (void) strlcpy(propbuf, "no", proplen); 2175789Sahrens break; 2176789Sahrens 2177789Sahrens case ZFS_PROP_NAME: 2178789Sahrens /* 2179789Sahrens * The 'name' property is a pseudo-property derived from the 2180789Sahrens * dataset name. It is presented as a real property to simplify 2181789Sahrens * consumers. 2182789Sahrens */ 2183789Sahrens (void) strlcpy(propbuf, zhp->zfs_name, proplen); 2184789Sahrens break; 2185789Sahrens 2186789Sahrens default: 21874577Sahrens switch (zfs_prop_get_type(prop)) { 21884787Sahrens case PROP_TYPE_NUMBER: 21894577Sahrens if (get_numeric_property(zhp, prop, src, 21904577Sahrens &source, &val) != 0) 21914577Sahrens return (-1); 21924577Sahrens if (literal) 21934577Sahrens (void) snprintf(propbuf, proplen, "%llu", 21944577Sahrens (u_longlong_t)val); 21954577Sahrens else 21964577Sahrens zfs_nicenum(val, propbuf, proplen); 21974577Sahrens break; 21984577Sahrens 21994787Sahrens case PROP_TYPE_STRING: 22004577Sahrens (void) strlcpy(propbuf, 22014577Sahrens getprop_string(zhp, prop, &source), proplen); 22024577Sahrens break; 22034577Sahrens 22044787Sahrens case PROP_TYPE_INDEX: 22054861Sahrens if (get_numeric_property(zhp, prop, src, 22064861Sahrens &source, &val) != 0) 22074861Sahrens return (-1); 22084861Sahrens if (zfs_prop_index_to_string(prop, val, &strval) != 0) 22094577Sahrens return (-1); 22104577Sahrens (void) strlcpy(propbuf, strval, proplen); 22114577Sahrens break; 22124577Sahrens 22134577Sahrens default: 22144577Sahrens abort(); 22154577Sahrens } 2216789Sahrens } 2217789Sahrens 2218789Sahrens get_source(zhp, src, source, statbuf, statlen); 2219789Sahrens 2220789Sahrens return (0); 2221789Sahrens } 2222789Sahrens 2223789Sahrens /* 2224789Sahrens * Utility function to get the given numeric property. Does no validation that 2225789Sahrens * the given property is the appropriate type; should only be used with 2226789Sahrens * hard-coded property types. 2227789Sahrens */ 2228789Sahrens uint64_t 2229789Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop) 2230789Sahrens { 2231789Sahrens char *source; 22325094Slling zprop_source_t sourcetype = ZPROP_SRC_NONE; 22332082Seschrock uint64_t val; 22342082Seschrock 22352082Seschrock (void) get_numeric_property(zhp, prop, &sourcetype, &source, &val); 22362082Seschrock 22372082Seschrock return (val); 2238789Sahrens } 2239789Sahrens 2240789Sahrens /* 2241789Sahrens * Similar to zfs_prop_get(), but returns the value as an integer. 2242789Sahrens */ 2243789Sahrens int 2244789Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value, 22455094Slling zprop_source_t *src, char *statbuf, size_t statlen) 2246789Sahrens { 2247789Sahrens char *source; 2248789Sahrens 2249789Sahrens /* 2250789Sahrens * Check to see if this property applies to our object 2251789Sahrens */ 22524849Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) { 22533237Slling return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE, 22542082Seschrock dgettext(TEXT_DOMAIN, "cannot get property '%s'"), 22552082Seschrock zfs_prop_to_name(prop))); 22564849Sahrens } 2257789Sahrens 2258789Sahrens if (src) 22595094Slling *src = ZPROP_SRC_NONE; 2260789Sahrens 22612082Seschrock if (get_numeric_property(zhp, prop, src, &source, value) != 0) 22622082Seschrock return (-1); 2263789Sahrens 2264789Sahrens get_source(zhp, src, source, statbuf, statlen); 2265789Sahrens 2266789Sahrens return (0); 2267789Sahrens } 2268789Sahrens 2269789Sahrens /* 2270789Sahrens * Returns the name of the given zfs handle. 2271789Sahrens */ 2272789Sahrens const char * 2273789Sahrens zfs_get_name(const zfs_handle_t *zhp) 2274789Sahrens { 2275789Sahrens return (zhp->zfs_name); 2276789Sahrens } 2277789Sahrens 2278789Sahrens /* 2279789Sahrens * Returns the type of the given zfs handle. 2280789Sahrens */ 2281789Sahrens zfs_type_t 2282789Sahrens zfs_get_type(const zfs_handle_t *zhp) 2283789Sahrens { 2284789Sahrens return (zhp->zfs_type); 2285789Sahrens } 2286789Sahrens 2287789Sahrens /* 22881356Seschrock * Iterate over all child filesystems 2289789Sahrens */ 2290789Sahrens int 22911356Seschrock zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data) 2292789Sahrens { 2293789Sahrens zfs_cmd_t zc = { 0 }; 2294789Sahrens zfs_handle_t *nzhp; 2295789Sahrens int ret; 2296789Sahrens 2297789Sahrens for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 22982082Seschrock ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_DATASET_LIST_NEXT, &zc) == 0; 2299789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) { 2300789Sahrens /* 2301789Sahrens * Ignore private dataset names. 2302789Sahrens */ 2303789Sahrens if (dataset_name_hidden(zc.zc_name)) 2304789Sahrens continue; 2305789Sahrens 2306789Sahrens /* 2307789Sahrens * Silently ignore errors, as the only plausible explanation is 2308789Sahrens * that the pool has since been removed. 2309789Sahrens */ 23102082Seschrock if ((nzhp = make_dataset_handle(zhp->zfs_hdl, 23112082Seschrock zc.zc_name)) == NULL) 2312789Sahrens continue; 2313789Sahrens 2314789Sahrens if ((ret = func(nzhp, data)) != 0) 2315789Sahrens return (ret); 2316789Sahrens } 2317789Sahrens 2318789Sahrens /* 2319789Sahrens * An errno value of ESRCH indicates normal completion. If ENOENT is 2320789Sahrens * returned, then the underlying dataset has been removed since we 2321789Sahrens * obtained the handle. 2322789Sahrens */ 2323789Sahrens if (errno != ESRCH && errno != ENOENT) 23242082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 23252082Seschrock dgettext(TEXT_DOMAIN, "cannot iterate filesystems"))); 2326789Sahrens 23271356Seschrock return (0); 23281356Seschrock } 23291356Seschrock 23301356Seschrock /* 23311356Seschrock * Iterate over all snapshots 23321356Seschrock */ 23331356Seschrock int 23341356Seschrock zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data) 23351356Seschrock { 23361356Seschrock zfs_cmd_t zc = { 0 }; 23371356Seschrock zfs_handle_t *nzhp; 23381356Seschrock int ret; 2339789Sahrens 2340789Sahrens for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 23412082Seschrock ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT, 23422082Seschrock &zc) == 0; 2343789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) { 2344789Sahrens 23452082Seschrock if ((nzhp = make_dataset_handle(zhp->zfs_hdl, 23462082Seschrock zc.zc_name)) == NULL) 2347789Sahrens continue; 2348789Sahrens 2349789Sahrens if ((ret = func(nzhp, data)) != 0) 2350789Sahrens return (ret); 2351789Sahrens } 2352789Sahrens 2353789Sahrens /* 2354789Sahrens * An errno value of ESRCH indicates normal completion. If ENOENT is 2355789Sahrens * returned, then the underlying dataset has been removed since we 2356789Sahrens * obtained the handle. Silently ignore this case, and return success. 2357789Sahrens */ 2358789Sahrens if (errno != ESRCH && errno != ENOENT) 23592082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 23602082Seschrock dgettext(TEXT_DOMAIN, "cannot iterate filesystems"))); 2361789Sahrens 2362789Sahrens return (0); 2363789Sahrens } 2364789Sahrens 2365789Sahrens /* 23661356Seschrock * Iterate over all children, snapshots and filesystems 23671356Seschrock */ 23681356Seschrock int 23691356Seschrock zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data) 23701356Seschrock { 23711356Seschrock int ret; 23721356Seschrock 23731356Seschrock if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0) 23741356Seschrock return (ret); 23751356Seschrock 23761356Seschrock return (zfs_iter_snapshots(zhp, func, data)); 23771356Seschrock } 23781356Seschrock 23791356Seschrock /* 2380789Sahrens * Given a complete name, return just the portion that refers to the parent. 2381789Sahrens * Can return NULL if this is a pool. 2382789Sahrens */ 2383789Sahrens static int 2384789Sahrens parent_name(const char *path, char *buf, size_t buflen) 2385789Sahrens { 2386789Sahrens char *loc; 2387789Sahrens 2388789Sahrens if ((loc = strrchr(path, '/')) == NULL) 2389789Sahrens return (-1); 2390789Sahrens 2391789Sahrens (void) strncpy(buf, path, MIN(buflen, loc - path)); 2392789Sahrens buf[loc - path] = '\0'; 2393789Sahrens 2394789Sahrens return (0); 2395789Sahrens } 2396789Sahrens 2397789Sahrens /* 23984490Svb160487 * If accept_ancestor is false, then check to make sure that the given path has 23994490Svb160487 * a parent, and that it exists. If accept_ancestor is true, then find the 24004490Svb160487 * closest existing ancestor for the given path. In prefixlen return the 24014490Svb160487 * length of already existing prefix of the given path. We also fetch the 24024490Svb160487 * 'zoned' property, which is used to validate property settings when creating 24034490Svb160487 * new datasets. 2404789Sahrens */ 2405789Sahrens static int 24064490Svb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned, 24074490Svb160487 boolean_t accept_ancestor, int *prefixlen) 2408789Sahrens { 2409789Sahrens zfs_cmd_t zc = { 0 }; 2410789Sahrens char parent[ZFS_MAXNAMELEN]; 2411789Sahrens char *slash; 24121356Seschrock zfs_handle_t *zhp; 24132082Seschrock char errbuf[1024]; 24142082Seschrock 24152082Seschrock (void) snprintf(errbuf, sizeof (errbuf), "cannot create '%s'", 24162082Seschrock path); 2417789Sahrens 2418789Sahrens /* get parent, and check to see if this is just a pool */ 2419789Sahrens if (parent_name(path, parent, sizeof (parent)) != 0) { 24202082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 24212082Seschrock "missing dataset name")); 24222082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2423789Sahrens } 2424789Sahrens 2425789Sahrens /* check to see if the pool exists */ 2426789Sahrens if ((slash = strchr(parent, '/')) == NULL) 2427789Sahrens slash = parent + strlen(parent); 2428789Sahrens (void) strncpy(zc.zc_name, parent, slash - parent); 2429789Sahrens zc.zc_name[slash - parent] = '\0'; 24302082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 && 2431789Sahrens errno == ENOENT) { 24322082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 24332082Seschrock "no such pool '%s'"), zc.zc_name); 24342082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2435789Sahrens } 2436789Sahrens 2437789Sahrens /* check to see if the parent dataset exists */ 24384490Svb160487 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) { 24394490Svb160487 if (errno == ENOENT && accept_ancestor) { 24404490Svb160487 /* 24414490Svb160487 * Go deeper to find an ancestor, give up on top level. 24424490Svb160487 */ 24434490Svb160487 if (parent_name(parent, parent, sizeof (parent)) != 0) { 24444490Svb160487 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 24454490Svb160487 "no such pool '%s'"), zc.zc_name); 24464490Svb160487 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 24474490Svb160487 } 24484490Svb160487 } else if (errno == ENOENT) { 24492082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 24502082Seschrock "parent does not exist")); 24512082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 24524490Svb160487 } else 24532082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 2454789Sahrens } 2455789Sahrens 24562676Seschrock *zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); 2457789Sahrens /* we are in a non-global zone, but parent is in the global zone */ 24582676Seschrock if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) { 24592082Seschrock (void) zfs_standard_error(hdl, EPERM, errbuf); 24601356Seschrock zfs_close(zhp); 2461789Sahrens return (-1); 2462789Sahrens } 2463789Sahrens 2464789Sahrens /* make sure parent is a filesystem */ 24651356Seschrock if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) { 24662082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 24672082Seschrock "parent is not a filesystem")); 24682082Seschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 24691356Seschrock zfs_close(zhp); 2470789Sahrens return (-1); 2471789Sahrens } 2472789Sahrens 24731356Seschrock zfs_close(zhp); 24744490Svb160487 if (prefixlen != NULL) 24754490Svb160487 *prefixlen = strlen(parent); 24764490Svb160487 return (0); 24774490Svb160487 } 24784490Svb160487 24794490Svb160487 /* 24804490Svb160487 * Finds whether the dataset of the given type(s) exists. 24814490Svb160487 */ 24824490Svb160487 boolean_t 24834490Svb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types) 24844490Svb160487 { 24854490Svb160487 zfs_handle_t *zhp; 24864490Svb160487 24875326Sek110237 if (!zfs_validate_name(hdl, path, types, B_FALSE)) 24884490Svb160487 return (B_FALSE); 24894490Svb160487 24904490Svb160487 /* 24914490Svb160487 * Try to get stats for the dataset, which will tell us if it exists. 24924490Svb160487 */ 24934490Svb160487 if ((zhp = make_dataset_handle(hdl, path)) != NULL) { 24944490Svb160487 int ds_type = zhp->zfs_type; 24954490Svb160487 24964490Svb160487 zfs_close(zhp); 24974490Svb160487 if (types & ds_type) 24984490Svb160487 return (B_TRUE); 24994490Svb160487 } 25004490Svb160487 return (B_FALSE); 25014490Svb160487 } 25024490Svb160487 25034490Svb160487 /* 25044490Svb160487 * Creates non-existing ancestors of the given path. 25054490Svb160487 */ 25064490Svb160487 int 25074490Svb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path) 25084490Svb160487 { 25094490Svb160487 int prefix; 25104490Svb160487 uint64_t zoned; 25114490Svb160487 char *path_copy; 25124490Svb160487 int rc; 25134490Svb160487 25144490Svb160487 if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0) 25154490Svb160487 return (-1); 25164490Svb160487 25174490Svb160487 if ((path_copy = strdup(path)) != NULL) { 25184490Svb160487 rc = create_parents(hdl, path_copy, prefix); 25194490Svb160487 free(path_copy); 25204490Svb160487 } 25214490Svb160487 if (path_copy == NULL || rc != 0) 25224490Svb160487 return (-1); 25234490Svb160487 2524789Sahrens return (0); 2525789Sahrens } 2526789Sahrens 2527789Sahrens /* 25282676Seschrock * Create a new filesystem or volume. 2529789Sahrens */ 2530789Sahrens int 25312082Seschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type, 25322676Seschrock nvlist_t *props) 2533789Sahrens { 2534789Sahrens zfs_cmd_t zc = { 0 }; 2535789Sahrens int ret; 2536789Sahrens uint64_t size = 0; 2537789Sahrens uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 25382082Seschrock char errbuf[1024]; 25392676Seschrock uint64_t zoned; 25402082Seschrock 25412082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 25422082Seschrock "cannot create '%s'"), path); 2543789Sahrens 2544789Sahrens /* validate the path, taking care to note the extended error message */ 25455326Sek110237 if (!zfs_validate_name(hdl, path, type, B_TRUE)) 25462082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2547789Sahrens 2548789Sahrens /* validate parents exist */ 25494490Svb160487 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0) 2550789Sahrens return (-1); 2551789Sahrens 2552789Sahrens /* 2553789Sahrens * The failure modes when creating a dataset of a different type over 2554789Sahrens * one that already exists is a little strange. In particular, if you 2555789Sahrens * try to create a dataset on top of an existing dataset, the ioctl() 2556789Sahrens * will return ENOENT, not EEXIST. To prevent this from happening, we 2557789Sahrens * first try to see if the dataset exists. 2558789Sahrens */ 2559789Sahrens (void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name)); 25605094Slling if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) { 25612082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 25622082Seschrock "dataset already exists")); 25632082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 2564789Sahrens } 2565789Sahrens 2566789Sahrens if (type == ZFS_TYPE_VOLUME) 2567789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 2568789Sahrens else 2569789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 2570789Sahrens 25715094Slling if (props && (props = zfs_validate_properties(hdl, type, props, 25723912Slling zoned, NULL, errbuf)) == 0) 25732676Seschrock return (-1); 25742676Seschrock 2575789Sahrens if (type == ZFS_TYPE_VOLUME) { 25761133Seschrock /* 25771133Seschrock * If we are creating a volume, the size and block size must 25781133Seschrock * satisfy a few restraints. First, the blocksize must be a 25791133Seschrock * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the 25801133Seschrock * volsize must be a multiple of the block size, and cannot be 25811133Seschrock * zero. 25821133Seschrock */ 25832676Seschrock if (props == NULL || nvlist_lookup_uint64(props, 25842676Seschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) { 25852676Seschrock nvlist_free(props); 25862082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 25872676Seschrock "missing volume size")); 25882676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 2589789Sahrens } 2590789Sahrens 25912676Seschrock if ((ret = nvlist_lookup_uint64(props, 25922676Seschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 25932676Seschrock &blocksize)) != 0) { 25942676Seschrock if (ret == ENOENT) { 25952676Seschrock blocksize = zfs_prop_default_numeric( 25962676Seschrock ZFS_PROP_VOLBLOCKSIZE); 25972676Seschrock } else { 25982676Seschrock nvlist_free(props); 25992676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26002676Seschrock "missing volume block size")); 26012676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 26022676Seschrock } 26032676Seschrock } 26042676Seschrock 26052676Seschrock if (size == 0) { 26062676Seschrock nvlist_free(props); 26072082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26082676Seschrock "volume size cannot be zero")); 26092676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 26101133Seschrock } 26111133Seschrock 26121133Seschrock if (size % blocksize != 0) { 26132676Seschrock nvlist_free(props); 26142082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26152676Seschrock "volume size must be a multiple of volume block " 26162676Seschrock "size")); 26172676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 26181133Seschrock } 2619789Sahrens } 2620789Sahrens 26215094Slling if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0) 26222676Seschrock return (-1); 26232676Seschrock nvlist_free(props); 26242676Seschrock 2625789Sahrens /* create the dataset */ 26264543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc); 2627789Sahrens 26283912Slling if (ret == 0 && type == ZFS_TYPE_VOLUME) { 26292082Seschrock ret = zvol_create_link(hdl, path); 26303912Slling if (ret) { 26313912Slling (void) zfs_standard_error(hdl, errno, 26323912Slling dgettext(TEXT_DOMAIN, 26333912Slling "Volume successfully created, but device links " 26343912Slling "were not created")); 26353912Slling zcmd_free_nvlists(&zc); 26363912Slling return (-1); 26373912Slling } 26383912Slling } 2639789Sahrens 26402676Seschrock zcmd_free_nvlists(&zc); 26412676Seschrock 2642789Sahrens /* check for failure */ 2643789Sahrens if (ret != 0) { 2644789Sahrens char parent[ZFS_MAXNAMELEN]; 2645789Sahrens (void) parent_name(path, parent, sizeof (parent)); 2646789Sahrens 2647789Sahrens switch (errno) { 2648789Sahrens case ENOENT: 26492082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26502082Seschrock "no such parent '%s'"), parent); 26512082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2652789Sahrens 2653789Sahrens case EINVAL: 26542082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26553413Smmusante "parent '%s' is not a filesystem"), parent); 26562082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 2657789Sahrens 2658789Sahrens case EDOM: 26592082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26602676Seschrock "volume block size must be power of 2 from " 26612676Seschrock "%u to %uk"), 2662789Sahrens (uint_t)SPA_MINBLOCKSIZE, 2663789Sahrens (uint_t)SPA_MAXBLOCKSIZE >> 10); 26642082Seschrock 26652676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 26662082Seschrock 26674603Sahrens case ENOTSUP: 26684603Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26694603Sahrens "pool must be upgraded to set this " 26704603Sahrens "property or value")); 26714603Sahrens return (zfs_error(hdl, EZFS_BADVERSION, errbuf)); 26724603Sahrens 2673789Sahrens #ifdef _ILP32 2674789Sahrens case EOVERFLOW: 2675789Sahrens /* 2676789Sahrens * This platform can't address a volume this big. 2677789Sahrens */ 26782082Seschrock if (type == ZFS_TYPE_VOLUME) 26792082Seschrock return (zfs_error(hdl, EZFS_VOLTOOBIG, 26802082Seschrock errbuf)); 2681789Sahrens #endif 26822082Seschrock /* FALLTHROUGH */ 2683789Sahrens default: 26842082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 2685789Sahrens } 2686789Sahrens } 2687789Sahrens 2688789Sahrens return (0); 2689789Sahrens } 2690789Sahrens 2691789Sahrens /* 2692789Sahrens * Destroys the given dataset. The caller must make sure that the filesystem 2693789Sahrens * isn't mounted, and that there are no active dependents. 2694789Sahrens */ 2695789Sahrens int 2696789Sahrens zfs_destroy(zfs_handle_t *zhp) 2697789Sahrens { 2698789Sahrens zfs_cmd_t zc = { 0 }; 2699789Sahrens 2700789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2701789Sahrens 27022676Seschrock if (ZFS_IS_VOLUME(zhp)) { 27033126Sahl /* 27044543Smarks * If user doesn't have permissions to unshare volume, then 27054543Smarks * abort the request. This would only happen for a 27064543Smarks * non-privileged user. 27073126Sahl */ 27084543Smarks if (zfs_unshare_iscsi(zhp) != 0) { 27094543Smarks return (-1); 27104543Smarks } 27113126Sahl 27122082Seschrock if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) 2713789Sahrens return (-1); 2714789Sahrens 2715789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 2716789Sahrens } else { 2717789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 2718789Sahrens } 2719789Sahrens 27204543Smarks if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) { 27213237Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, errno, 27222082Seschrock dgettext(TEXT_DOMAIN, "cannot destroy '%s'"), 27232082Seschrock zhp->zfs_name)); 27242199Sahrens } 2725789Sahrens 2726789Sahrens remove_mountpoint(zhp); 2727789Sahrens 2728789Sahrens return (0); 2729789Sahrens } 2730789Sahrens 27312199Sahrens struct destroydata { 27322199Sahrens char *snapname; 27332199Sahrens boolean_t gotone; 27343265Sahrens boolean_t closezhp; 27352199Sahrens }; 27362199Sahrens 27372199Sahrens static int 27382199Sahrens zfs_remove_link_cb(zfs_handle_t *zhp, void *arg) 27392199Sahrens { 27402199Sahrens struct destroydata *dd = arg; 27412199Sahrens zfs_handle_t *szhp; 27422199Sahrens char name[ZFS_MAXNAMELEN]; 27433265Sahrens boolean_t closezhp = dd->closezhp; 27443265Sahrens int rv; 27452199Sahrens 27462676Seschrock (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 27472676Seschrock (void) strlcat(name, "@", sizeof (name)); 27482676Seschrock (void) strlcat(name, dd->snapname, sizeof (name)); 27492199Sahrens 27502199Sahrens szhp = make_dataset_handle(zhp->zfs_hdl, name); 27512199Sahrens if (szhp) { 27522199Sahrens dd->gotone = B_TRUE; 27532199Sahrens zfs_close(szhp); 27542199Sahrens } 27552199Sahrens 27562199Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 27572199Sahrens (void) zvol_remove_link(zhp->zfs_hdl, name); 27582199Sahrens /* 27592199Sahrens * NB: this is simply a best-effort. We don't want to 27602199Sahrens * return an error, because then we wouldn't visit all 27612199Sahrens * the volumes. 27622199Sahrens */ 27632199Sahrens } 27642199Sahrens 27653265Sahrens dd->closezhp = B_TRUE; 27663265Sahrens rv = zfs_iter_filesystems(zhp, zfs_remove_link_cb, arg); 27673265Sahrens if (closezhp) 27683265Sahrens zfs_close(zhp); 27693265Sahrens return (rv); 27702199Sahrens } 27712199Sahrens 27722199Sahrens /* 27732199Sahrens * Destroys all snapshots with the given name in zhp & descendants. 27742199Sahrens */ 27752199Sahrens int 27762199Sahrens zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname) 27772199Sahrens { 27782199Sahrens zfs_cmd_t zc = { 0 }; 27792199Sahrens int ret; 27802199Sahrens struct destroydata dd = { 0 }; 27812199Sahrens 27822199Sahrens dd.snapname = snapname; 27832199Sahrens (void) zfs_remove_link_cb(zhp, &dd); 27842199Sahrens 27852199Sahrens if (!dd.gotone) { 27863237Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT, 27872199Sahrens dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"), 27882199Sahrens zhp->zfs_name, snapname)); 27892199Sahrens } 27902199Sahrens 27912199Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 27922676Seschrock (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value)); 27932199Sahrens 27944543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc); 27952199Sahrens if (ret != 0) { 27962199Sahrens char errbuf[1024]; 27972199Sahrens 27982199Sahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 27992199Sahrens "cannot destroy '%s@%s'"), zc.zc_name, snapname); 28002199Sahrens 28012199Sahrens switch (errno) { 28022199Sahrens case EEXIST: 28032199Sahrens zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 28042199Sahrens "snapshot is cloned")); 28052199Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf)); 28062199Sahrens 28072199Sahrens default: 28082199Sahrens return (zfs_standard_error(zhp->zfs_hdl, errno, 28092199Sahrens errbuf)); 28102199Sahrens } 28112199Sahrens } 28122199Sahrens 28132199Sahrens return (0); 28142199Sahrens } 28152199Sahrens 2816789Sahrens /* 2817789Sahrens * Clones the given dataset. The target must be of the same type as the source. 2818789Sahrens */ 2819789Sahrens int 28202676Seschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props) 2821789Sahrens { 2822789Sahrens zfs_cmd_t zc = { 0 }; 2823789Sahrens char parent[ZFS_MAXNAMELEN]; 2824789Sahrens int ret; 28252082Seschrock char errbuf[1024]; 28262082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 28272676Seschrock zfs_type_t type; 28282676Seschrock uint64_t zoned; 2829789Sahrens 2830789Sahrens assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 2831789Sahrens 28322082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 28332082Seschrock "cannot create '%s'"), target); 28342082Seschrock 2835789Sahrens /* validate the target name */ 28365326Sek110237 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE)) 28372082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2838789Sahrens 2839789Sahrens /* validate parents exist */ 28404490Svb160487 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0) 2841789Sahrens return (-1); 2842789Sahrens 2843789Sahrens (void) parent_name(target, parent, sizeof (parent)); 2844789Sahrens 2845789Sahrens /* do the clone */ 28462676Seschrock if (ZFS_IS_VOLUME(zhp)) { 2847789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 28482744Snn35248 type = ZFS_TYPE_VOLUME; 28492676Seschrock } else { 2850789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 28512744Snn35248 type = ZFS_TYPE_FILESYSTEM; 28522676Seschrock } 28532676Seschrock 28542676Seschrock if (props) { 28555094Slling if ((props = zfs_validate_properties(hdl, type, props, 28563912Slling zoned, zhp, errbuf)) == NULL) 28572676Seschrock return (-1); 28582676Seschrock 28595094Slling if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) { 28602676Seschrock nvlist_free(props); 28612676Seschrock return (-1); 28622676Seschrock } 28632676Seschrock 28642676Seschrock nvlist_free(props); 28652676Seschrock } 2866789Sahrens 2867789Sahrens (void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name)); 28682676Seschrock (void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value)); 28694543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc); 2870789Sahrens 28712676Seschrock zcmd_free_nvlists(&zc); 28722676Seschrock 2873789Sahrens if (ret != 0) { 2874789Sahrens switch (errno) { 2875789Sahrens 2876789Sahrens case ENOENT: 2877789Sahrens /* 2878789Sahrens * The parent doesn't exist. We should have caught this 2879789Sahrens * above, but there may a race condition that has since 2880789Sahrens * destroyed the parent. 2881789Sahrens * 2882789Sahrens * At this point, we don't know whether it's the source 2883789Sahrens * that doesn't exist anymore, or whether the target 2884789Sahrens * dataset doesn't exist. 2885789Sahrens */ 28862082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 28872082Seschrock "no such parent '%s'"), parent); 28882082Seschrock return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf)); 28892082Seschrock 28902082Seschrock case EXDEV: 28912082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 28922082Seschrock "source and target pools differ")); 28932082Seschrock return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET, 28942082Seschrock errbuf)); 28952082Seschrock 28962082Seschrock default: 28972082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 28982082Seschrock errbuf)); 28992082Seschrock } 29002676Seschrock } else if (ZFS_IS_VOLUME(zhp)) { 29012082Seschrock ret = zvol_create_link(zhp->zfs_hdl, target); 29022082Seschrock } 29032082Seschrock 29042082Seschrock return (ret); 29052082Seschrock } 29062082Seschrock 29072082Seschrock typedef struct promote_data { 29082082Seschrock char cb_mountpoint[MAXPATHLEN]; 29092082Seschrock const char *cb_target; 29102082Seschrock const char *cb_errbuf; 29112082Seschrock uint64_t cb_pivot_txg; 29122082Seschrock } promote_data_t; 29132082Seschrock 29142082Seschrock static int 29152082Seschrock promote_snap_cb(zfs_handle_t *zhp, void *data) 29162082Seschrock { 29172082Seschrock promote_data_t *pd = data; 29182082Seschrock zfs_handle_t *szhp; 29192082Seschrock char snapname[MAXPATHLEN]; 29203265Sahrens int rv = 0; 29212082Seschrock 29222082Seschrock /* We don't care about snapshots after the pivot point */ 29233265Sahrens if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) { 29243265Sahrens zfs_close(zhp); 29252082Seschrock return (0); 29263265Sahrens } 29272082Seschrock 29282417Sahrens /* Remove the device link if it's a zvol. */ 29292676Seschrock if (ZFS_IS_VOLUME(zhp)) 29302417Sahrens (void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name); 29312082Seschrock 29322082Seschrock /* Check for conflicting names */ 29332676Seschrock (void) strlcpy(snapname, pd->cb_target, sizeof (snapname)); 29342676Seschrock (void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname)); 29352082Seschrock szhp = make_dataset_handle(zhp->zfs_hdl, snapname); 29362082Seschrock if (szhp != NULL) { 29372082Seschrock zfs_close(szhp); 29382082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 29392082Seschrock "snapshot name '%s' from origin \n" 29402082Seschrock "conflicts with '%s' from target"), 29412082Seschrock zhp->zfs_name, snapname); 29423265Sahrens rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf); 29432082Seschrock } 29443265Sahrens zfs_close(zhp); 29453265Sahrens return (rv); 29462082Seschrock } 29472082Seschrock 29482417Sahrens static int 29492417Sahrens promote_snap_done_cb(zfs_handle_t *zhp, void *data) 29502417Sahrens { 29512417Sahrens promote_data_t *pd = data; 29522417Sahrens 29532417Sahrens /* We don't care about snapshots after the pivot point */ 29543265Sahrens if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) { 29553265Sahrens /* Create the device link if it's a zvol. */ 29563265Sahrens if (ZFS_IS_VOLUME(zhp)) 29573265Sahrens (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 29583265Sahrens } 29593265Sahrens 29603265Sahrens zfs_close(zhp); 29612417Sahrens return (0); 29622417Sahrens } 29632417Sahrens 29642082Seschrock /* 29652082Seschrock * Promotes the given clone fs to be the clone parent. 29662082Seschrock */ 29672082Seschrock int 29682082Seschrock zfs_promote(zfs_handle_t *zhp) 29692082Seschrock { 29702082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 29712082Seschrock zfs_cmd_t zc = { 0 }; 29722082Seschrock char parent[MAXPATHLEN]; 29732082Seschrock char *cp; 29742082Seschrock int ret; 29752082Seschrock zfs_handle_t *pzhp; 29762082Seschrock promote_data_t pd; 29772082Seschrock char errbuf[1024]; 29782082Seschrock 29792082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 29802082Seschrock "cannot promote '%s'"), zhp->zfs_name); 29812082Seschrock 29822082Seschrock if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 29832082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29842082Seschrock "snapshots can not be promoted")); 29852082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 29862082Seschrock } 29872082Seschrock 29882676Seschrock (void) strlcpy(parent, zhp->zfs_dmustats.dds_clone_of, sizeof (parent)); 29892082Seschrock if (parent[0] == '\0') { 29902082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29912082Seschrock "not a cloned filesystem")); 29922082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 29932082Seschrock } 29942082Seschrock cp = strchr(parent, '@'); 29952082Seschrock *cp = '\0'; 29962082Seschrock 29972082Seschrock /* Walk the snapshots we will be moving */ 29982082Seschrock pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_clone_of, ZFS_TYPE_SNAPSHOT); 29992082Seschrock if (pzhp == NULL) 30002082Seschrock return (-1); 30012082Seschrock pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG); 30022082Seschrock zfs_close(pzhp); 30032082Seschrock pd.cb_target = zhp->zfs_name; 30042082Seschrock pd.cb_errbuf = errbuf; 30055094Slling pzhp = zfs_open(hdl, parent, ZFS_TYPE_DATASET); 30062082Seschrock if (pzhp == NULL) 30072082Seschrock return (-1); 30082082Seschrock (void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint, 30092082Seschrock sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE); 30102082Seschrock ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd); 30112417Sahrens if (ret != 0) { 30122417Sahrens zfs_close(pzhp); 30132082Seschrock return (-1); 30142417Sahrens } 30152082Seschrock 30162082Seschrock /* issue the ioctl */ 30172676Seschrock (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_clone_of, 30182676Seschrock sizeof (zc.zc_value)); 30192082Seschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 30204543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc); 30212082Seschrock 30222082Seschrock if (ret != 0) { 30232417Sahrens int save_errno = errno; 30242417Sahrens 30252417Sahrens (void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd); 30262417Sahrens zfs_close(pzhp); 30272417Sahrens 30282417Sahrens switch (save_errno) { 3029789Sahrens case EEXIST: 3030789Sahrens /* 30312082Seschrock * There is a conflicting snapshot name. We 30322082Seschrock * should have caught this above, but they could 30332082Seschrock * have renamed something in the mean time. 3034789Sahrens */ 30352082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 30362082Seschrock "conflicting snapshot name from parent '%s'"), 30372082Seschrock parent); 30382082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3039789Sahrens 3040789Sahrens default: 30412417Sahrens return (zfs_standard_error(hdl, save_errno, errbuf)); 3042789Sahrens } 30432417Sahrens } else { 30442417Sahrens (void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd); 3045789Sahrens } 3046789Sahrens 30472417Sahrens zfs_close(pzhp); 3048789Sahrens return (ret); 3049789Sahrens } 3050789Sahrens 30514007Smmusante struct createdata { 30524007Smmusante const char *cd_snapname; 30534007Smmusante int cd_ifexists; 30544007Smmusante }; 30554007Smmusante 30562199Sahrens static int 30572199Sahrens zfs_create_link_cb(zfs_handle_t *zhp, void *arg) 30582199Sahrens { 30594007Smmusante struct createdata *cd = arg; 30602676Seschrock int ret; 30612199Sahrens 30622199Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 30632199Sahrens char name[MAXPATHLEN]; 30642199Sahrens 30652676Seschrock (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 30662676Seschrock (void) strlcat(name, "@", sizeof (name)); 30674007Smmusante (void) strlcat(name, cd->cd_snapname, sizeof (name)); 30684007Smmusante (void) zvol_create_link_common(zhp->zfs_hdl, name, 30694007Smmusante cd->cd_ifexists); 30702199Sahrens /* 30712199Sahrens * NB: this is simply a best-effort. We don't want to 30722199Sahrens * return an error, because then we wouldn't visit all 30732199Sahrens * the volumes. 30742199Sahrens */ 30752199Sahrens } 30762676Seschrock 30774007Smmusante ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd); 30782676Seschrock 30792676Seschrock zfs_close(zhp); 30802676Seschrock 30812676Seschrock return (ret); 30822199Sahrens } 30832199Sahrens 3084789Sahrens /* 30853504Sahl * Takes a snapshot of the given dataset. 3086789Sahrens */ 3087789Sahrens int 30882199Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive) 3089789Sahrens { 3090789Sahrens const char *delim; 3091789Sahrens char *parent; 3092789Sahrens zfs_handle_t *zhp; 3093789Sahrens zfs_cmd_t zc = { 0 }; 3094789Sahrens int ret; 30952082Seschrock char errbuf[1024]; 30962082Seschrock 30972082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 30982082Seschrock "cannot snapshot '%s'"), path); 30992082Seschrock 31002082Seschrock /* validate the target name */ 31015326Sek110237 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE)) 31022082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3103789Sahrens 3104789Sahrens /* make sure the parent exists and is of the appropriate type */ 31052199Sahrens delim = strchr(path, '@'); 31062082Seschrock if ((parent = zfs_alloc(hdl, delim - path + 1)) == NULL) 31072082Seschrock return (-1); 3108789Sahrens (void) strncpy(parent, path, delim - path); 3109789Sahrens parent[delim - path] = '\0'; 3110789Sahrens 31112082Seschrock if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM | 3112789Sahrens ZFS_TYPE_VOLUME)) == NULL) { 3113789Sahrens free(parent); 3114789Sahrens return (-1); 3115789Sahrens } 3116789Sahrens 31172199Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 31182676Seschrock (void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value)); 31194543Smarks if (ZFS_IS_VOLUME(zhp)) 31204543Smarks zc.zc_objset_type = DMU_OST_ZVOL; 31214543Smarks else 31224543Smarks zc.zc_objset_type = DMU_OST_ZFS; 31232199Sahrens zc.zc_cookie = recursive; 31244543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc); 31252199Sahrens 31262199Sahrens /* 31272199Sahrens * if it was recursive, the one that actually failed will be in 31282199Sahrens * zc.zc_name. 31292199Sahrens */ 31304543Smarks if (ret != 0) 31314543Smarks (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 31324543Smarks "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value); 31334543Smarks 31342199Sahrens if (ret == 0 && recursive) { 31354007Smmusante struct createdata cd; 31364007Smmusante 31374007Smmusante cd.cd_snapname = delim + 1; 31384007Smmusante cd.cd_ifexists = B_FALSE; 31394007Smmusante (void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd); 31402199Sahrens } 3141789Sahrens if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) { 31422082Seschrock ret = zvol_create_link(zhp->zfs_hdl, path); 31432199Sahrens if (ret != 0) { 31444543Smarks (void) zfs_standard_error(hdl, errno, 31454543Smarks dgettext(TEXT_DOMAIN, 31464543Smarks "Volume successfully snapshotted, but device links " 31474543Smarks "were not created")); 31484543Smarks free(parent); 31494543Smarks zfs_close(zhp); 31504543Smarks return (-1); 31512199Sahrens } 3152789Sahrens } 3153789Sahrens 31542082Seschrock if (ret != 0) 31552082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 3156789Sahrens 3157789Sahrens free(parent); 3158789Sahrens zfs_close(zhp); 3159789Sahrens 3160789Sahrens return (ret); 3161789Sahrens } 3162789Sahrens 3163789Sahrens /* 31643504Sahl * Dumps a backup of the given snapshot (incremental from fromsnap if it's not 31653504Sahl * NULL) to the file descriptor specified by outfd. 3166789Sahrens */ 3167789Sahrens int 31683504Sahl zfs_send(zfs_handle_t *zhp, const char *fromsnap, int outfd) 3169789Sahrens { 3170789Sahrens zfs_cmd_t zc = { 0 }; 31712082Seschrock char errbuf[1024]; 31722885Sahrens libzfs_handle_t *hdl = zhp->zfs_hdl; 31732082Seschrock 31743504Sahl assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 31753504Sahl 31762885Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 31772885Sahrens if (fromsnap) 31782885Sahrens (void) strlcpy(zc.zc_value, fromsnap, sizeof (zc.zc_name)); 31793504Sahl zc.zc_cookie = outfd; 31803504Sahl 31813504Sahl if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SENDBACKUP, &zc) != 0) { 31823504Sahl (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 31833504Sahl "cannot send '%s'"), zhp->zfs_name); 31843504Sahl 3185789Sahrens switch (errno) { 3186789Sahrens 3187789Sahrens case EXDEV: 31882082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 31893413Smmusante "not an earlier snapshot from the same fs")); 31902082Seschrock return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 3191789Sahrens 3192789Sahrens case EDQUOT: 3193789Sahrens case EFBIG: 3194789Sahrens case EIO: 3195789Sahrens case ENOLINK: 3196789Sahrens case ENOSPC: 3197789Sahrens case ENOSTR: 3198789Sahrens case ENXIO: 3199789Sahrens case EPIPE: 3200789Sahrens case ERANGE: 3201789Sahrens case EFAULT: 3202789Sahrens case EROFS: 32032082Seschrock zfs_error_aux(hdl, strerror(errno)); 32042082Seschrock return (zfs_error(hdl, EZFS_BADBACKUP, errbuf)); 3205789Sahrens 3206789Sahrens default: 32072082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 3208789Sahrens } 3209789Sahrens } 3210789Sahrens 32113504Sahl return (0); 3212789Sahrens } 3213789Sahrens 3214789Sahrens /* 32152885Sahrens * Create ancestors of 'target', but not target itself, and not 32162885Sahrens * ancestors whose names are shorter than prefixlen. Die if 32172885Sahrens * prefixlen-ancestor does not exist. 32182885Sahrens */ 32192885Sahrens static int 32202885Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen) 32212885Sahrens { 32222885Sahrens zfs_handle_t *h; 32232885Sahrens char *cp; 32242885Sahrens 32252885Sahrens /* make sure prefix exists */ 32262885Sahrens cp = strchr(target + prefixlen, '/'); 32274603Sahrens if (cp == NULL) { 32284603Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 32294603Sahrens } else { 32304603Sahrens *cp = '\0'; 32314603Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 32324603Sahrens *cp = '/'; 32334603Sahrens } 32342885Sahrens if (h == NULL) 32352885Sahrens return (-1); 32362885Sahrens zfs_close(h); 32372885Sahrens 32382885Sahrens /* 32392885Sahrens * Attempt to create, mount, and share any ancestor filesystems, 32402885Sahrens * up to the prefixlen-long one. 32412885Sahrens */ 32422885Sahrens for (cp = target + prefixlen + 1; 32432885Sahrens cp = strchr(cp, '/'); *cp = '/', cp++) { 32442885Sahrens const char *opname; 32454543Smarks char *logstr; 32462885Sahrens 32472885Sahrens *cp = '\0'; 32482885Sahrens 32492885Sahrens h = make_dataset_handle(hdl, target); 32502885Sahrens if (h) { 32512885Sahrens /* it already exists, nothing to do here */ 32522885Sahrens zfs_close(h); 32532885Sahrens continue; 32542885Sahrens } 32552885Sahrens 32562885Sahrens opname = dgettext(TEXT_DOMAIN, "create"); 32574543Smarks logstr = hdl->libzfs_log_str; 32584543Smarks hdl->libzfs_log_str = NULL; 32592885Sahrens if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM, 32604543Smarks NULL) != 0) { 32614543Smarks hdl->libzfs_log_str = logstr; 32622885Sahrens goto ancestorerr; 32634543Smarks } 32644543Smarks 32654543Smarks hdl->libzfs_log_str = logstr; 32662885Sahrens opname = dgettext(TEXT_DOMAIN, "open"); 32672885Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 32682885Sahrens if (h == NULL) 32692885Sahrens goto ancestorerr; 32702885Sahrens 32712885Sahrens opname = dgettext(TEXT_DOMAIN, "mount"); 32722885Sahrens if (zfs_mount(h, NULL, 0) != 0) 32732885Sahrens goto ancestorerr; 32742885Sahrens 32752885Sahrens opname = dgettext(TEXT_DOMAIN, "share"); 32762885Sahrens if (zfs_share(h) != 0) 32772885Sahrens goto ancestorerr; 32782885Sahrens 32792885Sahrens zfs_close(h); 32802885Sahrens 32812885Sahrens continue; 32822885Sahrens ancestorerr: 32832885Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 32842885Sahrens "failed to %s ancestor '%s'"), opname, target); 32852885Sahrens return (-1); 32862885Sahrens } 32872885Sahrens 32882885Sahrens return (0); 32892885Sahrens } 32902885Sahrens 32912885Sahrens /* 32923504Sahl * Restores a backup of tosnap from the file descriptor specified by infd. 3293789Sahrens */ 3294789Sahrens int 32952082Seschrock zfs_receive(libzfs_handle_t *hdl, const char *tosnap, int isprefix, 32963504Sahl int verbose, int dryrun, boolean_t force, int infd) 3297789Sahrens { 3298789Sahrens zfs_cmd_t zc = { 0 }; 3299789Sahrens time_t begin_time; 33002885Sahrens int ioctl_err, err, bytes, size, choplen; 3301789Sahrens char *cp; 3302789Sahrens dmu_replay_record_t drr; 3303789Sahrens struct drr_begin *drrb = &zc.zc_begin_record; 33042082Seschrock char errbuf[1024]; 33052885Sahrens char chopprefix[ZFS_MAXNAMELEN]; 3306789Sahrens 3307789Sahrens begin_time = time(NULL); 3308789Sahrens 33092082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 33102082Seschrock "cannot receive")); 33112082Seschrock 3312789Sahrens /* read in the BEGIN record */ 3313789Sahrens cp = (char *)&drr; 3314789Sahrens bytes = 0; 3315789Sahrens do { 33163504Sahl size = read(infd, cp, sizeof (drr) - bytes); 3317868Sahrens cp += size; 3318868Sahrens bytes += size; 3319868Sahrens } while (size > 0); 3320868Sahrens 3321868Sahrens if (size < 0 || bytes != sizeof (drr)) { 33222082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 33232082Seschrock "stream (failed to read first record)")); 33242082Seschrock return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 3325789Sahrens } 3326789Sahrens 3327789Sahrens zc.zc_begin_record = drr.drr_u.drr_begin; 3328789Sahrens 3329789Sahrens if (drrb->drr_magic != DMU_BACKUP_MAGIC && 3330789Sahrens drrb->drr_magic != BSWAP_64(DMU_BACKUP_MAGIC)) { 33312082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 33322082Seschrock "stream (bad magic number)")); 33332082Seschrock return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 3334789Sahrens } 3335789Sahrens 3336789Sahrens if (drrb->drr_version != DMU_BACKUP_VERSION && 3337789Sahrens drrb->drr_version != BSWAP_64(DMU_BACKUP_VERSION)) { 33382082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only version " 33392082Seschrock "0x%llx is supported (stream is version 0x%llx)"), 3340789Sahrens DMU_BACKUP_VERSION, drrb->drr_version); 33412082Seschrock return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 3342789Sahrens } 3343789Sahrens 33442885Sahrens if (strchr(drr.drr_u.drr_begin.drr_toname, '@') == NULL) { 33452885Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 33463912Slling "stream (bad snapshot name)")); 33472885Sahrens return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 33482885Sahrens } 3349789Sahrens /* 33502885Sahrens * Determine how much of the snapshot name stored in the stream 33512885Sahrens * we are going to tack on to the name they specified on the 33522885Sahrens * command line, and how much we are going to chop off. 33532885Sahrens * 33542885Sahrens * If they specified a snapshot, chop the entire name stored in 33552885Sahrens * the stream. 3356789Sahrens */ 33572885Sahrens (void) strcpy(chopprefix, drr.drr_u.drr_begin.drr_toname); 3358789Sahrens if (isprefix) { 33592885Sahrens /* 33602885Sahrens * They specified a fs with -d, we want to tack on 33612885Sahrens * everything but the pool name stored in the stream 33622885Sahrens */ 33632885Sahrens if (strchr(tosnap, '@')) { 33642885Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 33652885Sahrens "argument - snapshot not allowed with -d")); 33662885Sahrens return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3367789Sahrens } 33682885Sahrens cp = strchr(chopprefix, '/'); 3369789Sahrens if (cp == NULL) 33702885Sahrens cp = strchr(chopprefix, '@'); 33712885Sahrens *cp = '\0'; 3372789Sahrens } else if (strchr(tosnap, '@') == NULL) { 3373789Sahrens /* 33742885Sahrens * If they specified a filesystem without -d, we want to 33752885Sahrens * tack on everything after the fs specified in the 33762885Sahrens * first name from the stream. 3377789Sahrens */ 33782885Sahrens cp = strchr(chopprefix, '@'); 33792885Sahrens *cp = '\0'; 3380789Sahrens } 33812885Sahrens choplen = strlen(chopprefix); 33822885Sahrens 33832885Sahrens /* 33842885Sahrens * Determine name of destination snapshot, store in zc_value. 33852885Sahrens */ 33862885Sahrens (void) strcpy(zc.zc_value, tosnap); 33872885Sahrens (void) strncat(zc.zc_value, drr.drr_u.drr_begin.drr_toname+choplen, 33882885Sahrens sizeof (zc.zc_value)); 33895326Sek110237 if (!zfs_validate_name(hdl, zc.zc_value, ZFS_TYPE_SNAPSHOT, B_TRUE)) 33903265Sahrens return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 33912885Sahrens 33922885Sahrens (void) strcpy(zc.zc_name, zc.zc_value); 3393789Sahrens if (drrb->drr_fromguid) { 3394789Sahrens /* incremental backup stream */ 33952885Sahrens zfs_handle_t *h; 33962885Sahrens 33972885Sahrens /* do the recvbackup ioctl to the containing fs */ 33982885Sahrens *strchr(zc.zc_name, '@') = '\0'; 3399789Sahrens 3400789Sahrens /* make sure destination fs exists */ 34012082Seschrock h = zfs_open(hdl, zc.zc_name, 34022082Seschrock ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 34032082Seschrock if (h == NULL) 3404789Sahrens return (-1); 34055326Sek110237 if (!dryrun && h->zfs_type == ZFS_TYPE_VOLUME) { 34065326Sek110237 if (zvol_remove_link(hdl, h->zfs_name) != 0) { 34075326Sek110237 zfs_close(h); 34085326Sek110237 return (-1); 3409868Sahrens } 3410868Sahrens } 3411789Sahrens zfs_close(h); 3412789Sahrens } else { 3413789Sahrens /* full backup stream */ 3414789Sahrens 3415868Sahrens /* Make sure destination fs does not exist */ 34162885Sahrens *strchr(zc.zc_name, '@') = '\0'; 34175094Slling if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) { 34182082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 34192082Seschrock "destination '%s' exists"), zc.zc_name); 34202082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3421868Sahrens } 3422868Sahrens 34232885Sahrens if (strchr(zc.zc_name, '/') == NULL) { 34242885Sahrens /* 34252885Sahrens * they're trying to do a recv into a 34262885Sahrens * nonexistant topmost filesystem. 34272885Sahrens */ 34282885Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 34292885Sahrens "destination does not exist"), zc.zc_name); 34302885Sahrens return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 34312885Sahrens } 34322885Sahrens 3433868Sahrens /* Do the recvbackup ioctl to the fs's parent. */ 34342885Sahrens *strrchr(zc.zc_name, '/') = '\0'; 34352885Sahrens 34362885Sahrens if (isprefix && (err = create_parents(hdl, 34372885Sahrens zc.zc_value, strlen(tosnap))) != 0) { 34382885Sahrens return (zfs_error(hdl, EZFS_BADRESTORE, errbuf)); 34392885Sahrens } 34402885Sahrens 3441789Sahrens } 3442789Sahrens 34433504Sahl zc.zc_cookie = infd; 34442676Seschrock zc.zc_guid = force; 3445789Sahrens if (verbose) { 34461749Sahrens (void) printf("%s %s stream of %s into %s\n", 34471749Sahrens dryrun ? "would receive" : "receiving", 3448789Sahrens drrb->drr_fromguid ? "incremental" : "full", 3449789Sahrens drr.drr_u.drr_begin.drr_toname, 34502676Seschrock zc.zc_value); 3451789Sahrens (void) fflush(stdout); 3452789Sahrens } 3453789Sahrens if (dryrun) 3454789Sahrens return (0); 34554543Smarks err = ioctl_err = zfs_ioctl(hdl, ZFS_IOC_RECVBACKUP, &zc); 3456868Sahrens if (ioctl_err != 0) { 3457789Sahrens switch (errno) { 3458789Sahrens case ENODEV: 34592082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 34602082Seschrock "most recent snapshot does not match incremental " 34612082Seschrock "source")); 34622082Seschrock (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf); 3463789Sahrens break; 3464789Sahrens case ETXTBSY: 34652082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 34662082Seschrock "destination has been modified since most recent " 34672082Seschrock "snapshot")); 34682082Seschrock (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf); 3469789Sahrens break; 3470789Sahrens case EEXIST: 3471789Sahrens if (drrb->drr_fromguid == 0) { 3472789Sahrens /* it's the containing fs that exists */ 34732676Seschrock cp = strchr(zc.zc_value, '@'); 3474789Sahrens *cp = '\0'; 3475789Sahrens } 34762082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 34772082Seschrock "destination already exists")); 34783237Slling (void) zfs_error_fmt(hdl, EZFS_EXISTS, 34793237Slling dgettext(TEXT_DOMAIN, "cannot restore to %s"), 34803237Slling zc.zc_value); 3481789Sahrens break; 3482789Sahrens case EINVAL: 34832082Seschrock (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf); 3484868Sahrens break; 34851544Seschrock case ECKSUM: 34862082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 34872082Seschrock "invalid stream (checksum mismatch)")); 34882082Seschrock (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf); 3489789Sahrens break; 3490789Sahrens default: 34912082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 3492789Sahrens } 3493789Sahrens } 3494789Sahrens 3495789Sahrens /* 3496868Sahrens * Mount or recreate the /dev links for the target filesystem 3497868Sahrens * (if created, or if we tore them down to do an incremental 3498868Sahrens * restore), and the /dev links for the new snapshot (if 34992665Snd150628 * created). Also mount any children of the target filesystem 35002665Snd150628 * if we did an incremental receive. 3501789Sahrens */ 35022676Seschrock cp = strchr(zc.zc_value, '@'); 3503868Sahrens if (cp && (ioctl_err == 0 || drrb->drr_fromguid)) { 3504789Sahrens zfs_handle_t *h; 3505789Sahrens 3506789Sahrens *cp = '\0'; 35072676Seschrock h = zfs_open(hdl, zc.zc_value, 3508789Sahrens ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 3509868Sahrens *cp = '@'; 3510789Sahrens if (h) { 35112665Snd150628 if (h->zfs_type == ZFS_TYPE_VOLUME) { 35122082Seschrock err = zvol_create_link(hdl, h->zfs_name); 35131544Seschrock if (err == 0 && ioctl_err == 0) 35142082Seschrock err = zvol_create_link(hdl, 35152676Seschrock zc.zc_value); 35165326Sek110237 } else if (!drrb->drr_fromguid) { 35175326Sek110237 err = zfs_mount(h, NULL, 0); 3518868Sahrens } 35192665Snd150628 zfs_close(h); 3520789Sahrens } 3521789Sahrens } 3522789Sahrens 3523868Sahrens if (err || ioctl_err) 3524868Sahrens return (-1); 3525789Sahrens 3526789Sahrens if (verbose) { 3527789Sahrens char buf1[64]; 3528789Sahrens char buf2[64]; 3529789Sahrens uint64_t bytes = zc.zc_cookie; 3530789Sahrens time_t delta = time(NULL) - begin_time; 3531789Sahrens if (delta == 0) 3532789Sahrens delta = 1; 3533789Sahrens zfs_nicenum(bytes, buf1, sizeof (buf1)); 3534789Sahrens zfs_nicenum(bytes/delta, buf2, sizeof (buf1)); 3535789Sahrens 35364603Sahrens (void) printf("received %sB stream in %lu seconds (%sB/sec)\n", 3537789Sahrens buf1, delta, buf2); 3538789Sahrens } 35392665Snd150628 3540789Sahrens return (0); 3541789Sahrens } 3542789Sahrens 3543789Sahrens /* 35441294Slling * Destroy any more recent snapshots. We invoke this callback on any dependents 35451294Slling * of the snapshot first. If the 'cb_dependent' member is non-zero, then this 35461294Slling * is a dependent and we should just destroy it without checking the transaction 35471294Slling * group. 3548789Sahrens */ 35491294Slling typedef struct rollback_data { 35501294Slling const char *cb_target; /* the snapshot */ 35511294Slling uint64_t cb_create; /* creation time reference */ 35521294Slling prop_changelist_t *cb_clp; /* changelist pointer */ 35531294Slling int cb_error; 35542082Seschrock boolean_t cb_dependent; 35551294Slling } rollback_data_t; 35561294Slling 35571294Slling static int 35581294Slling rollback_destroy(zfs_handle_t *zhp, void *data) 35591294Slling { 35601294Slling rollback_data_t *cbp = data; 35611294Slling 35621294Slling if (!cbp->cb_dependent) { 35631294Slling if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 && 35641294Slling zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT && 35651294Slling zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > 35661294Slling cbp->cb_create) { 35674543Smarks char *logstr; 35681294Slling 35692082Seschrock cbp->cb_dependent = B_TRUE; 35702474Seschrock if (zfs_iter_dependents(zhp, B_FALSE, rollback_destroy, 35712474Seschrock cbp) != 0) 35722474Seschrock cbp->cb_error = 1; 35732082Seschrock cbp->cb_dependent = B_FALSE; 35741294Slling 35754543Smarks logstr = zhp->zfs_hdl->libzfs_log_str; 35764543Smarks zhp->zfs_hdl->libzfs_log_str = NULL; 35771294Slling if (zfs_destroy(zhp) != 0) 35781294Slling cbp->cb_error = 1; 35791294Slling else 35801294Slling changelist_remove(zhp, cbp->cb_clp); 35814543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 35821294Slling } 35831294Slling } else { 35841294Slling if (zfs_destroy(zhp) != 0) 35851294Slling cbp->cb_error = 1; 35861294Slling else 35871294Slling changelist_remove(zhp, cbp->cb_clp); 35881294Slling } 35891294Slling 35901294Slling zfs_close(zhp); 35911294Slling return (0); 35921294Slling } 35931294Slling 35941294Slling /* 35951294Slling * Rollback the dataset to its latest snapshot. 35961294Slling */ 35971294Slling static int 35981294Slling do_rollback(zfs_handle_t *zhp) 3599789Sahrens { 3600789Sahrens int ret; 3601789Sahrens zfs_cmd_t zc = { 0 }; 3602789Sahrens 3603789Sahrens assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM || 3604789Sahrens zhp->zfs_type == ZFS_TYPE_VOLUME); 3605789Sahrens 3606789Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME && 36072082Seschrock zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) 3608789Sahrens return (-1); 3609789Sahrens 3610789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3611789Sahrens 36122676Seschrock if (ZFS_IS_VOLUME(zhp)) 3613789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3614789Sahrens else 3615789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3616789Sahrens 3617789Sahrens /* 3618789Sahrens * We rely on the consumer to verify that there are no newer snapshots 3619789Sahrens * for the given dataset. Given these constraints, we can simply pass 3620789Sahrens * the name on to the ioctl() call. There is still an unlikely race 3621789Sahrens * condition where the user has taken a snapshot since we verified that 3622789Sahrens * this was the most recent. 3623789Sahrens */ 36244543Smarks if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) { 36253237Slling (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno, 36262082Seschrock dgettext(TEXT_DOMAIN, "cannot rollback '%s'"), 36272082Seschrock zhp->zfs_name); 3628789Sahrens } else if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 36292082Seschrock ret = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 3630789Sahrens } 3631789Sahrens 3632789Sahrens return (ret); 3633789Sahrens } 3634789Sahrens 3635789Sahrens /* 36361294Slling * Given a dataset, rollback to a specific snapshot, discarding any 36371294Slling * data changes since then and making it the active dataset. 36381294Slling * 36391294Slling * Any snapshots more recent than the target are destroyed, along with 36401294Slling * their dependents. 36411294Slling */ 36421294Slling int 36431294Slling zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, int flag) 36441294Slling { 36451294Slling int ret; 36461294Slling rollback_data_t cb = { 0 }; 36471294Slling prop_changelist_t *clp; 36481294Slling 36491294Slling /* 36501294Slling * Unmount all dependendents of the dataset and the dataset itself. 36511294Slling * The list we need to gather is the same as for doing rename 36521294Slling */ 36531294Slling clp = changelist_gather(zhp, ZFS_PROP_NAME, flag ? MS_FORCE: 0); 36541294Slling if (clp == NULL) 36551294Slling return (-1); 36561294Slling 36571294Slling if ((ret = changelist_prefix(clp)) != 0) 36581294Slling goto out; 36591294Slling 36601294Slling /* 36611294Slling * Destroy all recent snapshots and its dependends. 36621294Slling */ 36631294Slling cb.cb_target = snap->zfs_name; 36641294Slling cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG); 36651294Slling cb.cb_clp = clp; 36661294Slling (void) zfs_iter_children(zhp, rollback_destroy, &cb); 36671294Slling 36681294Slling if ((ret = cb.cb_error) != 0) { 36691294Slling (void) changelist_postfix(clp); 36701294Slling goto out; 36711294Slling } 36721294Slling 36731294Slling /* 36741294Slling * Now that we have verified that the snapshot is the latest, 36751294Slling * rollback to the given snapshot. 36761294Slling */ 36771294Slling ret = do_rollback(zhp); 36781294Slling 36791294Slling if (ret != 0) { 36801294Slling (void) changelist_postfix(clp); 36811294Slling goto out; 36821294Slling } 36831294Slling 36841294Slling /* 36851294Slling * We only want to re-mount the filesystem if it was mounted in the 36861294Slling * first place. 36871294Slling */ 36881294Slling ret = changelist_postfix(clp); 36891294Slling 36901294Slling out: 36911294Slling changelist_free(clp); 36921294Slling return (ret); 36931294Slling } 36941294Slling 36951294Slling /* 3696789Sahrens * Iterate over all dependents for a given dataset. This includes both 3697789Sahrens * hierarchical dependents (children) and data dependents (snapshots and 3698789Sahrens * clones). The bulk of the processing occurs in get_dependents() in 3699789Sahrens * libzfs_graph.c. 3700789Sahrens */ 3701789Sahrens int 37022474Seschrock zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion, 37032474Seschrock zfs_iter_f func, void *data) 3704789Sahrens { 3705789Sahrens char **dependents; 3706789Sahrens size_t count; 3707789Sahrens int i; 3708789Sahrens zfs_handle_t *child; 3709789Sahrens int ret = 0; 3710789Sahrens 37112474Seschrock if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name, 37122474Seschrock &dependents, &count) != 0) 37132474Seschrock return (-1); 37142474Seschrock 3715789Sahrens for (i = 0; i < count; i++) { 37162082Seschrock if ((child = make_dataset_handle(zhp->zfs_hdl, 37172082Seschrock dependents[i])) == NULL) 3718789Sahrens continue; 3719789Sahrens 3720789Sahrens if ((ret = func(child, data)) != 0) 3721789Sahrens break; 3722789Sahrens } 3723789Sahrens 3724789Sahrens for (i = 0; i < count; i++) 3725789Sahrens free(dependents[i]); 3726789Sahrens free(dependents); 3727789Sahrens 3728789Sahrens return (ret); 3729789Sahrens } 3730789Sahrens 3731789Sahrens /* 3732789Sahrens * Renames the given dataset. 3733789Sahrens */ 3734789Sahrens int 37354490Svb160487 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive) 3736789Sahrens { 3737789Sahrens int ret; 3738789Sahrens zfs_cmd_t zc = { 0 }; 3739789Sahrens char *delim; 37404007Smmusante prop_changelist_t *cl = NULL; 37414007Smmusante zfs_handle_t *zhrp = NULL; 37424007Smmusante char *parentname = NULL; 3743789Sahrens char parent[ZFS_MAXNAMELEN]; 37442082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 37452082Seschrock char errbuf[1024]; 3746789Sahrens 3747789Sahrens /* if we have the same exact name, just return success */ 3748789Sahrens if (strcmp(zhp->zfs_name, target) == 0) 3749789Sahrens return (0); 3750789Sahrens 37512082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 37522082Seschrock "cannot rename to '%s'"), target); 37532082Seschrock 3754789Sahrens /* 3755789Sahrens * Make sure the target name is valid 3756789Sahrens */ 3757789Sahrens if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 37582665Snd150628 if ((strchr(target, '@') == NULL) || 37592665Snd150628 *target == '@') { 37602665Snd150628 /* 37612665Snd150628 * Snapshot target name is abbreviated, 37622665Snd150628 * reconstruct full dataset name 37632665Snd150628 */ 37642665Snd150628 (void) strlcpy(parent, zhp->zfs_name, 37652665Snd150628 sizeof (parent)); 37662665Snd150628 delim = strchr(parent, '@'); 37672665Snd150628 if (strchr(target, '@') == NULL) 37682665Snd150628 *(++delim) = '\0'; 37692665Snd150628 else 37702665Snd150628 *delim = '\0'; 37712665Snd150628 (void) strlcat(parent, target, sizeof (parent)); 37722665Snd150628 target = parent; 37732665Snd150628 } else { 37742665Snd150628 /* 37752665Snd150628 * Make sure we're renaming within the same dataset. 37762665Snd150628 */ 37772665Snd150628 delim = strchr(target, '@'); 37782665Snd150628 if (strncmp(zhp->zfs_name, target, delim - target) 37792665Snd150628 != 0 || zhp->zfs_name[delim - target] != '@') { 37802665Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37812665Snd150628 "snapshots must be part of same " 37822665Snd150628 "dataset")); 37832665Snd150628 return (zfs_error(hdl, EZFS_CROSSTARGET, 37843912Slling errbuf)); 37852665Snd150628 } 3786789Sahrens } 37875326Sek110237 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 37882665Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3789789Sahrens } else { 37904007Smmusante if (recursive) { 37914007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37924007Smmusante "recursive rename must be a snapshot")); 37934007Smmusante return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 37944007Smmusante } 37954007Smmusante 37965326Sek110237 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 37972665Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 37982676Seschrock uint64_t unused; 37992676Seschrock 3800789Sahrens /* validate parents */ 38014490Svb160487 if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0) 3802789Sahrens return (-1); 3803789Sahrens 3804789Sahrens (void) parent_name(target, parent, sizeof (parent)); 3805789Sahrens 3806789Sahrens /* make sure we're in the same pool */ 3807789Sahrens verify((delim = strchr(target, '/')) != NULL); 3808789Sahrens if (strncmp(zhp->zfs_name, target, delim - target) != 0 || 3809789Sahrens zhp->zfs_name[delim - target] != '/') { 38102082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 38112082Seschrock "datasets must be within same pool")); 38122082Seschrock return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 3813789Sahrens } 38142440Snd150628 38152440Snd150628 /* new name cannot be a child of the current dataset name */ 38162440Snd150628 if (strncmp(parent, zhp->zfs_name, 38173912Slling strlen(zhp->zfs_name)) == 0) { 38182440Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 38192440Snd150628 "New dataset name cannot be a descendent of " 38202440Snd150628 "current dataset name")); 38212440Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 38222440Snd150628 } 3823789Sahrens } 3824789Sahrens 38252082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 38262082Seschrock dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name); 38272082Seschrock 3828789Sahrens if (getzoneid() == GLOBAL_ZONEID && 3829789Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 38302082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 38312082Seschrock "dataset is used in a non-global zone")); 38322082Seschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 3833789Sahrens } 3834789Sahrens 38354007Smmusante if (recursive) { 38364007Smmusante struct destroydata dd; 38374007Smmusante 38384183Smmusante parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name); 38394183Smmusante if (parentname == NULL) { 38404183Smmusante ret = -1; 38414183Smmusante goto error; 38424183Smmusante } 38434007Smmusante delim = strchr(parentname, '@'); 38444007Smmusante *delim = '\0'; 38455094Slling zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET); 38464007Smmusante if (zhrp == NULL) { 38474183Smmusante ret = -1; 38484183Smmusante goto error; 38494007Smmusante } 38504007Smmusante 38514007Smmusante dd.snapname = delim + 1; 38524007Smmusante dd.gotone = B_FALSE; 38534183Smmusante dd.closezhp = B_TRUE; 38544007Smmusante 38554007Smmusante /* We remove any zvol links prior to renaming them */ 38564007Smmusante ret = zfs_iter_filesystems(zhrp, zfs_remove_link_cb, &dd); 38574007Smmusante if (ret) { 38584007Smmusante goto error; 38594007Smmusante } 38604007Smmusante } else { 38614007Smmusante if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0)) == NULL) 38624007Smmusante return (-1); 38634007Smmusante 38644007Smmusante if (changelist_haszonedchild(cl)) { 38654007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 38664007Smmusante "child dataset with inherited mountpoint is used " 38674007Smmusante "in a non-global zone")); 38684007Smmusante (void) zfs_error(hdl, EZFS_ZONED, errbuf); 38694007Smmusante goto error; 38704007Smmusante } 38714007Smmusante 38724007Smmusante if ((ret = changelist_prefix(cl)) != 0) 38734007Smmusante goto error; 3874789Sahrens } 3875789Sahrens 38762676Seschrock if (ZFS_IS_VOLUME(zhp)) 3877789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3878789Sahrens else 3879789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3880789Sahrens 38812665Snd150628 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 38822676Seschrock (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value)); 38832665Snd150628 38844007Smmusante zc.zc_cookie = recursive; 38854007Smmusante 38864543Smarks if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) { 38874007Smmusante /* 38884007Smmusante * if it was recursive, the one that actually failed will 38894007Smmusante * be in zc.zc_name 38904007Smmusante */ 38914007Smmusante (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 38924007Smmusante "cannot rename to '%s'"), zc.zc_name); 38934007Smmusante 38944007Smmusante if (recursive && errno == EEXIST) { 38954007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 38964007Smmusante "a child dataset already has a snapshot " 38974007Smmusante "with the new name")); 38984801Seschrock (void) zfs_error(hdl, EZFS_EXISTS, errbuf); 38994007Smmusante } else { 39004007Smmusante (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf); 39014007Smmusante } 3902789Sahrens 3903789Sahrens /* 3904789Sahrens * On failure, we still want to remount any filesystems that 3905789Sahrens * were previously mounted, so we don't alter the system state. 3906789Sahrens */ 39074007Smmusante if (recursive) { 39084007Smmusante struct createdata cd; 39094007Smmusante 39104007Smmusante /* only create links for datasets that had existed */ 39114007Smmusante cd.cd_snapname = delim + 1; 39124007Smmusante cd.cd_ifexists = B_TRUE; 39134007Smmusante (void) zfs_iter_filesystems(zhrp, zfs_create_link_cb, 39144007Smmusante &cd); 39154007Smmusante } else { 39164007Smmusante (void) changelist_postfix(cl); 39174007Smmusante } 3918789Sahrens } else { 39194007Smmusante if (recursive) { 39204007Smmusante struct createdata cd; 39214007Smmusante 39224007Smmusante /* only create links for datasets that had existed */ 39234007Smmusante cd.cd_snapname = strchr(target, '@') + 1; 39244007Smmusante cd.cd_ifexists = B_TRUE; 39254007Smmusante ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb, 39264007Smmusante &cd); 39274007Smmusante } else { 39284007Smmusante changelist_rename(cl, zfs_get_name(zhp), target); 39294007Smmusante ret = changelist_postfix(cl); 39304007Smmusante } 3931789Sahrens } 3932789Sahrens 3933789Sahrens error: 39344007Smmusante if (parentname) { 39354007Smmusante free(parentname); 39364007Smmusante } 39374007Smmusante if (zhrp) { 39384007Smmusante zfs_close(zhrp); 39394007Smmusante } 39404007Smmusante if (cl) { 39414007Smmusante changelist_free(cl); 39424007Smmusante } 3943789Sahrens return (ret); 3944789Sahrens } 3945789Sahrens 3946789Sahrens /* 3947789Sahrens * Given a zvol dataset, issue the ioctl to create the appropriate minor node, 3948789Sahrens * poke devfsadm to create the /dev link, and then wait for the link to appear. 3949789Sahrens */ 3950789Sahrens int 39512082Seschrock zvol_create_link(libzfs_handle_t *hdl, const char *dataset) 3952789Sahrens { 39534007Smmusante return (zvol_create_link_common(hdl, dataset, B_FALSE)); 39544007Smmusante } 39554007Smmusante 39564007Smmusante static int 39574007Smmusante zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists) 39584007Smmusante { 3959789Sahrens zfs_cmd_t zc = { 0 }; 39602082Seschrock di_devlink_handle_t dhdl; 39614543Smarks priv_set_t *priv_effective; 39624543Smarks int privileged; 3963789Sahrens 3964789Sahrens (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 3965789Sahrens 3966789Sahrens /* 3967789Sahrens * Issue the appropriate ioctl. 3968789Sahrens */ 39692082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) { 3970789Sahrens switch (errno) { 3971789Sahrens case EEXIST: 3972789Sahrens /* 3973789Sahrens * Silently ignore the case where the link already 3974789Sahrens * exists. This allows 'zfs volinit' to be run multiple 3975789Sahrens * times without errors. 3976789Sahrens */ 3977789Sahrens return (0); 3978789Sahrens 39794007Smmusante case ENOENT: 39804007Smmusante /* 39814007Smmusante * Dataset does not exist in the kernel. If we 39824007Smmusante * don't care (see zfs_rename), then ignore the 39834007Smmusante * error quietly. 39844007Smmusante */ 39854007Smmusante if (ifexists) { 39864007Smmusante return (0); 39874007Smmusante } 39884007Smmusante 39894007Smmusante /* FALLTHROUGH */ 39904007Smmusante 3991789Sahrens default: 39923237Slling return (zfs_standard_error_fmt(hdl, errno, 39932082Seschrock dgettext(TEXT_DOMAIN, "cannot create device links " 39942082Seschrock "for '%s'"), dataset)); 3995789Sahrens } 3996789Sahrens } 3997789Sahrens 3998789Sahrens /* 39994543Smarks * If privileged call devfsadm and wait for the links to 40004543Smarks * magically appear. 40014543Smarks * Otherwise, print out an informational message. 4002789Sahrens */ 40034543Smarks 40044543Smarks priv_effective = priv_allocset(); 40054543Smarks (void) getppriv(PRIV_EFFECTIVE, priv_effective); 40064543Smarks privileged = (priv_isfullset(priv_effective) == B_TRUE); 40074543Smarks priv_freeset(priv_effective); 40084543Smarks 40094543Smarks if (privileged) { 40104543Smarks if ((dhdl = di_devlink_init(ZFS_DRIVER, 40114543Smarks DI_MAKE_LINK)) == NULL) { 40124543Smarks zfs_error_aux(hdl, strerror(errno)); 40134543Smarks (void) zfs_standard_error_fmt(hdl, EZFS_DEVLINKS, 40144543Smarks dgettext(TEXT_DOMAIN, "cannot create device links " 40154543Smarks "for '%s'"), dataset); 40164543Smarks (void) ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc); 40174543Smarks return (-1); 40184543Smarks } else { 40194543Smarks (void) di_devlink_fini(&dhdl); 40204543Smarks } 4021789Sahrens } else { 40224543Smarks char pathname[MAXPATHLEN]; 40234543Smarks struct stat64 statbuf; 40244543Smarks int i; 40254543Smarks 40264543Smarks #define MAX_WAIT 10 40274543Smarks 40284543Smarks /* 40294543Smarks * This is the poor mans way of waiting for the link 40304543Smarks * to show up. If after 10 seconds we still don't 40314543Smarks * have it, then print out a message. 40324543Smarks */ 40334543Smarks (void) snprintf(pathname, sizeof (pathname), "/dev/zvol/dsk/%s", 40344543Smarks dataset); 40354543Smarks 40364543Smarks for (i = 0; i != MAX_WAIT; i++) { 40374543Smarks if (stat64(pathname, &statbuf) == 0) 40384543Smarks break; 40394543Smarks (void) sleep(1); 40404543Smarks } 40414543Smarks if (i == MAX_WAIT) 40424543Smarks (void) printf(gettext("%s may not be immediately " 40434543Smarks "available\n"), pathname); 4044789Sahrens } 4045789Sahrens 4046789Sahrens return (0); 4047789Sahrens } 4048789Sahrens 4049789Sahrens /* 4050789Sahrens * Remove a minor node for the given zvol and the associated /dev links. 4051789Sahrens */ 4052789Sahrens int 40532082Seschrock zvol_remove_link(libzfs_handle_t *hdl, const char *dataset) 4054789Sahrens { 4055789Sahrens zfs_cmd_t zc = { 0 }; 4056789Sahrens 4057789Sahrens (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 4058789Sahrens 40592082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) { 4060789Sahrens switch (errno) { 4061789Sahrens case ENXIO: 4062789Sahrens /* 4063789Sahrens * Silently ignore the case where the link no longer 4064789Sahrens * exists, so that 'zfs volfini' can be run multiple 4065789Sahrens * times without errors. 4066789Sahrens */ 4067789Sahrens return (0); 4068789Sahrens 4069789Sahrens default: 40703237Slling return (zfs_standard_error_fmt(hdl, errno, 40712082Seschrock dgettext(TEXT_DOMAIN, "cannot remove device " 40722082Seschrock "links for '%s'"), dataset)); 4073789Sahrens } 4074789Sahrens } 4075789Sahrens 4076789Sahrens return (0); 4077789Sahrens } 40782676Seschrock 40792676Seschrock nvlist_t * 40802676Seschrock zfs_get_user_props(zfs_handle_t *zhp) 40812676Seschrock { 40822676Seschrock return (zhp->zfs_user_props); 40832676Seschrock } 40842676Seschrock 40852676Seschrock /* 40863912Slling * This function is used by 'zfs list' to determine the exact set of columns to 40873912Slling * display, and their maximum widths. This does two main things: 40883912Slling * 40893912Slling * - If this is a list of all properties, then expand the list to include 40903912Slling * all native properties, and set a flag so that for each dataset we look 40913912Slling * for new unique user properties and add them to the list. 40923912Slling * 40933912Slling * - For non fixed-width properties, keep track of the maximum width seen 40943912Slling * so that we can size the column appropriately. 40953912Slling */ 40963912Slling int 40975094Slling zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp) 40983912Slling { 40993912Slling libzfs_handle_t *hdl = zhp->zfs_hdl; 41005094Slling zprop_list_t *entry; 41015094Slling zprop_list_t **last, **start; 41023912Slling nvlist_t *userprops, *propval; 41033912Slling nvpair_t *elem; 41043912Slling char *strval; 41053912Slling char buf[ZFS_MAXPROPLEN]; 41063912Slling 41075094Slling if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0) 41083912Slling return (-1); 41092676Seschrock 41102676Seschrock userprops = zfs_get_user_props(zhp); 41112676Seschrock 41122676Seschrock entry = *plp; 41132676Seschrock if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) { 41142676Seschrock /* 41152676Seschrock * Go through and add any user properties as necessary. We 41162676Seschrock * start by incrementing our list pointer to the first 41172676Seschrock * non-native property. 41182676Seschrock */ 41192676Seschrock start = plp; 41202676Seschrock while (*start != NULL) { 41215094Slling if ((*start)->pl_prop == ZPROP_INVAL) 41222676Seschrock break; 41232676Seschrock start = &(*start)->pl_next; 41242676Seschrock } 41252676Seschrock 41262676Seschrock elem = NULL; 41272676Seschrock while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) { 41282676Seschrock /* 41292676Seschrock * See if we've already found this property in our list. 41302676Seschrock */ 41312676Seschrock for (last = start; *last != NULL; 41322676Seschrock last = &(*last)->pl_next) { 41332676Seschrock if (strcmp((*last)->pl_user_prop, 41342676Seschrock nvpair_name(elem)) == 0) 41352676Seschrock break; 41362676Seschrock } 41372676Seschrock 41382676Seschrock if (*last == NULL) { 41392676Seschrock if ((entry = zfs_alloc(hdl, 41405094Slling sizeof (zprop_list_t))) == NULL || 41412676Seschrock ((entry->pl_user_prop = zfs_strdup(hdl, 41422676Seschrock nvpair_name(elem)))) == NULL) { 41432676Seschrock free(entry); 41442676Seschrock return (-1); 41452676Seschrock } 41462676Seschrock 41475094Slling entry->pl_prop = ZPROP_INVAL; 41482676Seschrock entry->pl_width = strlen(nvpair_name(elem)); 41492676Seschrock entry->pl_all = B_TRUE; 41502676Seschrock *last = entry; 41512676Seschrock } 41522676Seschrock } 41532676Seschrock } 41542676Seschrock 41552676Seschrock /* 41562676Seschrock * Now go through and check the width of any non-fixed columns 41572676Seschrock */ 41582676Seschrock for (entry = *plp; entry != NULL; entry = entry->pl_next) { 41592676Seschrock if (entry->pl_fixed) 41602676Seschrock continue; 41612676Seschrock 41625094Slling if (entry->pl_prop != ZPROP_INVAL) { 41632676Seschrock if (zfs_prop_get(zhp, entry->pl_prop, 41642676Seschrock buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) { 41652676Seschrock if (strlen(buf) > entry->pl_width) 41662676Seschrock entry->pl_width = strlen(buf); 41672676Seschrock } 41682676Seschrock } else if (nvlist_lookup_nvlist(userprops, 41692676Seschrock entry->pl_user_prop, &propval) == 0) { 41702676Seschrock verify(nvlist_lookup_string(propval, 41715094Slling ZPROP_VALUE, &strval) == 0); 41722676Seschrock if (strlen(strval) > entry->pl_width) 41732676Seschrock entry->pl_width = strlen(strval); 41742676Seschrock } 41752676Seschrock } 41762676Seschrock 41772676Seschrock return (0); 41782676Seschrock } 41794543Smarks 41804543Smarks int 41814543Smarks zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred) 41824543Smarks { 41834543Smarks zfs_cmd_t zc = { 0 }; 41844543Smarks nvlist_t *nvp; 41854543Smarks gid_t gid; 41864543Smarks uid_t uid; 41874543Smarks const gid_t *groups; 41884543Smarks int group_cnt; 41894543Smarks int error; 41904543Smarks 41914543Smarks if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0) 41924543Smarks return (no_memory(hdl)); 41934543Smarks 41944543Smarks uid = ucred_geteuid(cred); 41954543Smarks gid = ucred_getegid(cred); 41964543Smarks group_cnt = ucred_getgroups(cred, &groups); 41974543Smarks 41984543Smarks if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1) 41994543Smarks return (1); 42004543Smarks 42014543Smarks if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) { 42024543Smarks nvlist_free(nvp); 42034543Smarks return (1); 42044543Smarks } 42054543Smarks 42064543Smarks if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) { 42074543Smarks nvlist_free(nvp); 42084543Smarks return (1); 42094543Smarks } 42104543Smarks 42114543Smarks if (nvlist_add_uint32_array(nvp, 42124543Smarks ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) { 42134543Smarks nvlist_free(nvp); 42144543Smarks return (1); 42154543Smarks } 42164543Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 42174543Smarks 42185094Slling if (zcmd_write_src_nvlist(hdl, &zc, nvp)) 42194543Smarks return (-1); 42204543Smarks 42214543Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc); 42224543Smarks nvlist_free(nvp); 42234543Smarks return (error); 42244543Smarks } 42254543Smarks 42264543Smarks int 42274543Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path, 4228*5331Samw void *export, void *sharetab, int sharemax, zfs_share_op_t operation) 42294543Smarks { 42304543Smarks zfs_cmd_t zc = { 0 }; 42314543Smarks int error; 42324543Smarks 42334543Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 42344543Smarks (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value)); 42354543Smarks zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab; 42364543Smarks zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export; 4237*5331Samw zc.zc_share.z_sharetype = operation; 42384543Smarks zc.zc_share.z_sharemax = sharemax; 42394543Smarks 42404543Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc); 42414543Smarks return (error); 42424543Smarks } 4243