1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51485Slling * Common Development and Distribution License (the "License"). 61485Slling * 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 */ 21789Sahrens /* 2211422SMark.Musante@Sun.COM * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 26789Sahrens #include <sys/types.h> 27789Sahrens #include <sys/param.h> 28789Sahrens #include <sys/errno.h> 29789Sahrens #include <sys/uio.h> 30789Sahrens #include <sys/buf.h> 31789Sahrens #include <sys/modctl.h> 32789Sahrens #include <sys/open.h> 33789Sahrens #include <sys/file.h> 34789Sahrens #include <sys/kmem.h> 35789Sahrens #include <sys/conf.h> 36789Sahrens #include <sys/cmn_err.h> 37789Sahrens #include <sys/stat.h> 38789Sahrens #include <sys/zfs_ioctl.h> 3910972SRic.Aleshire@Sun.COM #include <sys/zfs_vfsops.h> 405331Samw #include <sys/zfs_znode.h> 41789Sahrens #include <sys/zap.h> 42789Sahrens #include <sys/spa.h> 433912Slling #include <sys/spa_impl.h> 44789Sahrens #include <sys/vdev.h> 4510972SRic.Aleshire@Sun.COM #include <sys/priv_impl.h> 46789Sahrens #include <sys/dmu.h> 47789Sahrens #include <sys/dsl_dir.h> 48789Sahrens #include <sys/dsl_dataset.h> 49789Sahrens #include <sys/dsl_prop.h> 504543Smarks #include <sys/dsl_deleg.h> 514543Smarks #include <sys/dmu_objset.h> 52789Sahrens #include <sys/ddi.h> 53789Sahrens #include <sys/sunddi.h> 54789Sahrens #include <sys/sunldi.h> 55789Sahrens #include <sys/policy.h> 56789Sahrens #include <sys/zone.h> 57789Sahrens #include <sys/nvpair.h> 58789Sahrens #include <sys/pathname.h> 59789Sahrens #include <sys/mount.h> 60789Sahrens #include <sys/sdt.h> 61789Sahrens #include <sys/fs/zfs.h> 62789Sahrens #include <sys/zfs_ctldir.h> 635331Samw #include <sys/zfs_dir.h> 642885Sahrens #include <sys/zvol.h> 654543Smarks #include <sharefs/share.h> 665326Sek110237 #include <sys/dmu_objset.h> 67789Sahrens 68789Sahrens #include "zfs_namecheck.h" 692676Seschrock #include "zfs_prop.h" 704543Smarks #include "zfs_deleg.h" 71789Sahrens 72789Sahrens extern struct modlfs zfs_modlfs; 73789Sahrens 74789Sahrens extern void zfs_init(void); 75789Sahrens extern void zfs_fini(void); 76789Sahrens 77789Sahrens ldi_ident_t zfs_li = NULL; 78789Sahrens dev_info_t *zfs_dip; 79789Sahrens 80789Sahrens typedef int zfs_ioc_func_t(zfs_cmd_t *); 814543Smarks typedef int zfs_secpolicy_func_t(zfs_cmd_t *, cred_t *); 82789Sahrens 839234SGeorge.Wilson@Sun.COM typedef enum { 849234SGeorge.Wilson@Sun.COM NO_NAME, 859234SGeorge.Wilson@Sun.COM POOL_NAME, 869234SGeorge.Wilson@Sun.COM DATASET_NAME 879234SGeorge.Wilson@Sun.COM } zfs_ioc_namecheck_t; 889234SGeorge.Wilson@Sun.COM 89789Sahrens typedef struct zfs_ioc_vec { 90789Sahrens zfs_ioc_func_t *zvec_func; 91789Sahrens zfs_secpolicy_func_t *zvec_secpolicy; 929234SGeorge.Wilson@Sun.COM zfs_ioc_namecheck_t zvec_namecheck; 934543Smarks boolean_t zvec_his_log; 949234SGeorge.Wilson@Sun.COM boolean_t zvec_pool_check; 95789Sahrens } zfs_ioc_vec_t; 96789Sahrens 979396SMatthew.Ahrens@Sun.COM /* This array is indexed by zfs_userquota_prop_t */ 989396SMatthew.Ahrens@Sun.COM static const char *userquota_perms[] = { 999396SMatthew.Ahrens@Sun.COM ZFS_DELEG_PERM_USERUSED, 1009396SMatthew.Ahrens@Sun.COM ZFS_DELEG_PERM_USERQUOTA, 1019396SMatthew.Ahrens@Sun.COM ZFS_DELEG_PERM_GROUPUSED, 1029396SMatthew.Ahrens@Sun.COM ZFS_DELEG_PERM_GROUPQUOTA, 1039396SMatthew.Ahrens@Sun.COM }; 1049396SMatthew.Ahrens@Sun.COM 1059396SMatthew.Ahrens@Sun.COM static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc); 10611022STom.Erickson@Sun.COM static int zfs_check_settable(const char *name, nvpair_t *property, 10711022STom.Erickson@Sun.COM cred_t *cr); 10811022STom.Erickson@Sun.COM static int zfs_check_clearable(char *dataset, nvlist_t *props, 10911022STom.Erickson@Sun.COM nvlist_t **errors); 1107184Stimh static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *, 1117184Stimh boolean_t *); 11211022STom.Erickson@Sun.COM int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t **); 1137184Stimh 114789Sahrens /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */ 115789Sahrens void 116789Sahrens __dprintf(const char *file, const char *func, int line, const char *fmt, ...) 117789Sahrens { 118789Sahrens const char *newfile; 119789Sahrens char buf[256]; 120789Sahrens va_list adx; 121789Sahrens 122789Sahrens /* 123789Sahrens * Get rid of annoying "../common/" prefix to filename. 124789Sahrens */ 125789Sahrens newfile = strrchr(file, '/'); 126789Sahrens if (newfile != NULL) { 127789Sahrens newfile = newfile + 1; /* Get rid of leading / */ 128789Sahrens } else { 129789Sahrens newfile = file; 130789Sahrens } 131789Sahrens 132789Sahrens va_start(adx, fmt); 133789Sahrens (void) vsnprintf(buf, sizeof (buf), fmt, adx); 134789Sahrens va_end(adx); 135789Sahrens 136789Sahrens /* 137789Sahrens * To get this data, use the zfs-dprintf probe as so: 138789Sahrens * dtrace -q -n 'zfs-dprintf \ 139789Sahrens * /stringof(arg0) == "dbuf.c"/ \ 140789Sahrens * {printf("%s: %s", stringof(arg1), stringof(arg3))}' 141789Sahrens * arg0 = file name 142789Sahrens * arg1 = function name 143789Sahrens * arg2 = line number 144789Sahrens * arg3 = message 145789Sahrens */ 146789Sahrens DTRACE_PROBE4(zfs__dprintf, 147789Sahrens char *, newfile, char *, func, int, line, char *, buf); 148789Sahrens } 149789Sahrens 1504543Smarks static void 1514715Sek110237 history_str_free(char *buf) 1524715Sek110237 { 1534715Sek110237 kmem_free(buf, HIS_MAX_RECORD_LEN); 1544715Sek110237 } 1554715Sek110237 1564715Sek110237 static char * 1574715Sek110237 history_str_get(zfs_cmd_t *zc) 1584715Sek110237 { 1594715Sek110237 char *buf; 1604715Sek110237 1614715Sek110237 if (zc->zc_history == NULL) 1624715Sek110237 return (NULL); 1634715Sek110237 1644715Sek110237 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP); 1654715Sek110237 if (copyinstr((void *)(uintptr_t)zc->zc_history, 1664715Sek110237 buf, HIS_MAX_RECORD_LEN, NULL) != 0) { 1674715Sek110237 history_str_free(buf); 1684715Sek110237 return (NULL); 1694715Sek110237 } 1704715Sek110237 1714715Sek110237 buf[HIS_MAX_RECORD_LEN -1] = '\0'; 1724715Sek110237 1734715Sek110237 return (buf); 1744715Sek110237 } 1754715Sek110237 1765375Stimh /* 1777042Sgw25295 * Check to see if the named dataset is currently defined as bootable 1787042Sgw25295 */ 1797042Sgw25295 static boolean_t 1807042Sgw25295 zfs_is_bootfs(const char *name) 1817042Sgw25295 { 18210298SMatthew.Ahrens@Sun.COM objset_t *os; 18310298SMatthew.Ahrens@Sun.COM 18410298SMatthew.Ahrens@Sun.COM if (dmu_objset_hold(name, FTAG, &os) == 0) { 18510298SMatthew.Ahrens@Sun.COM boolean_t ret; 18610922SJeff.Bonwick@Sun.COM ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os))); 18710298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 18810298SMatthew.Ahrens@Sun.COM return (ret); 1897042Sgw25295 } 19010298SMatthew.Ahrens@Sun.COM return (B_FALSE); 1917042Sgw25295 } 1927042Sgw25295 1937042Sgw25295 /* 1947184Stimh * zfs_earlier_version 1955375Stimh * 1965375Stimh * Return non-zero if the spa version is less than requested version. 1975375Stimh */ 1985331Samw static int 1997184Stimh zfs_earlier_version(const char *name, int version) 2005331Samw { 2015331Samw spa_t *spa; 2025331Samw 2035331Samw if (spa_open(name, &spa, FTAG) == 0) { 2045331Samw if (spa_version(spa) < version) { 2055331Samw spa_close(spa, FTAG); 2065331Samw return (1); 2075331Samw } 2085331Samw spa_close(spa, FTAG); 2095331Samw } 2105331Samw return (0); 2115331Samw } 2125331Samw 2135977Smarks /* 2146689Smaybee * zpl_earlier_version 2155977Smarks * 2166689Smaybee * Return TRUE if the ZPL version is less than requested version. 2175977Smarks */ 2186689Smaybee static boolean_t 2196689Smaybee zpl_earlier_version(const char *name, int version) 2205977Smarks { 2215977Smarks objset_t *os; 2226689Smaybee boolean_t rc = B_TRUE; 2235977Smarks 22410298SMatthew.Ahrens@Sun.COM if (dmu_objset_hold(name, FTAG, &os) == 0) { 2256689Smaybee uint64_t zplversion; 2266689Smaybee 22710298SMatthew.Ahrens@Sun.COM if (dmu_objset_type(os) != DMU_OST_ZFS) { 22810298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 22910298SMatthew.Ahrens@Sun.COM return (B_TRUE); 23010298SMatthew.Ahrens@Sun.COM } 23110298SMatthew.Ahrens@Sun.COM /* XXX reading from non-owned objset */ 2326689Smaybee if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0) 2336689Smaybee rc = zplversion < version; 23410298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 2355977Smarks } 2365977Smarks return (rc); 2375977Smarks } 2385977Smarks 2394715Sek110237 static void 2404543Smarks zfs_log_history(zfs_cmd_t *zc) 2414543Smarks { 2424543Smarks spa_t *spa; 2434603Sahrens char *buf; 2444543Smarks 2454715Sek110237 if ((buf = history_str_get(zc)) == NULL) 2464577Sahrens return; 2474577Sahrens 2484715Sek110237 if (spa_open(zc->zc_name, &spa, FTAG) == 0) { 2494715Sek110237 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY) 2504715Sek110237 (void) spa_history_log(spa, buf, LOG_CMD_NORMAL); 2514715Sek110237 spa_close(spa, FTAG); 2524543Smarks } 2534715Sek110237 history_str_free(buf); 2544543Smarks } 2554543Smarks 256789Sahrens /* 257789Sahrens * Policy for top-level read operations (list pools). Requires no privileges, 258789Sahrens * and can be used in the local zone, as there is no associated dataset. 259789Sahrens */ 260789Sahrens /* ARGSUSED */ 261789Sahrens static int 2624543Smarks zfs_secpolicy_none(zfs_cmd_t *zc, cred_t *cr) 263789Sahrens { 264789Sahrens return (0); 265789Sahrens } 266789Sahrens 267789Sahrens /* 268789Sahrens * Policy for dataset read operations (list children, get statistics). Requires 269789Sahrens * no privileges, but must be visible in the local zone. 270789Sahrens */ 271789Sahrens /* ARGSUSED */ 272789Sahrens static int 2734543Smarks zfs_secpolicy_read(zfs_cmd_t *zc, cred_t *cr) 274789Sahrens { 275789Sahrens if (INGLOBALZONE(curproc) || 2764543Smarks zone_dataset_visible(zc->zc_name, NULL)) 277789Sahrens return (0); 278789Sahrens 279789Sahrens return (ENOENT); 280789Sahrens } 281789Sahrens 282789Sahrens static int 283789Sahrens zfs_dozonecheck(const char *dataset, cred_t *cr) 284789Sahrens { 285789Sahrens uint64_t zoned; 286789Sahrens int writable = 1; 287789Sahrens 288789Sahrens /* 289789Sahrens * The dataset must be visible by this zone -- check this first 290789Sahrens * so they don't see EPERM on something they shouldn't know about. 291789Sahrens */ 292789Sahrens if (!INGLOBALZONE(curproc) && 293789Sahrens !zone_dataset_visible(dataset, &writable)) 294789Sahrens return (ENOENT); 295789Sahrens 296789Sahrens if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL)) 297789Sahrens return (ENOENT); 298789Sahrens 299789Sahrens if (INGLOBALZONE(curproc)) { 300789Sahrens /* 301789Sahrens * If the fs is zoned, only root can access it from the 302789Sahrens * global zone. 303789Sahrens */ 304789Sahrens if (secpolicy_zfs(cr) && zoned) 305789Sahrens return (EPERM); 306789Sahrens } else { 307789Sahrens /* 308789Sahrens * If we are in a local zone, the 'zoned' property must be set. 309789Sahrens */ 310789Sahrens if (!zoned) 311789Sahrens return (EPERM); 312789Sahrens 313789Sahrens /* must be writable by this zone */ 314789Sahrens if (!writable) 315789Sahrens return (EPERM); 316789Sahrens } 317789Sahrens return (0); 318789Sahrens } 319789Sahrens 320789Sahrens int 3214543Smarks zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr) 322789Sahrens { 323789Sahrens int error; 324789Sahrens 3254543Smarks error = zfs_dozonecheck(name, cr); 3264543Smarks if (error == 0) { 3274543Smarks error = secpolicy_zfs(cr); 3284670Sahrens if (error) 3294543Smarks error = dsl_deleg_access(name, perm, cr); 3304543Smarks } 3314543Smarks return (error); 3324543Smarks } 3334543Smarks 33410972SRic.Aleshire@Sun.COM /* 33510972SRic.Aleshire@Sun.COM * Policy for setting the security label property. 33610972SRic.Aleshire@Sun.COM * 33710972SRic.Aleshire@Sun.COM * Returns 0 for success, non-zero for access and other errors. 33810972SRic.Aleshire@Sun.COM */ 33910972SRic.Aleshire@Sun.COM static int 34011022STom.Erickson@Sun.COM zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr) 34110972SRic.Aleshire@Sun.COM { 34210972SRic.Aleshire@Sun.COM char ds_hexsl[MAXNAMELEN]; 34310972SRic.Aleshire@Sun.COM bslabel_t ds_sl, new_sl; 34410972SRic.Aleshire@Sun.COM boolean_t new_default = FALSE; 34510972SRic.Aleshire@Sun.COM uint64_t zoned; 34610972SRic.Aleshire@Sun.COM int needed_priv = -1; 34710972SRic.Aleshire@Sun.COM int error; 34810972SRic.Aleshire@Sun.COM 34910972SRic.Aleshire@Sun.COM /* First get the existing dataset label. */ 35010972SRic.Aleshire@Sun.COM error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL), 35110972SRic.Aleshire@Sun.COM 1, sizeof (ds_hexsl), &ds_hexsl, NULL); 35210972SRic.Aleshire@Sun.COM if (error) 35310972SRic.Aleshire@Sun.COM return (EPERM); 35410972SRic.Aleshire@Sun.COM 35510972SRic.Aleshire@Sun.COM if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0) 35610972SRic.Aleshire@Sun.COM new_default = TRUE; 35710972SRic.Aleshire@Sun.COM 35810972SRic.Aleshire@Sun.COM /* The label must be translatable */ 35910972SRic.Aleshire@Sun.COM if (!new_default && (hexstr_to_label(strval, &new_sl) != 0)) 36010972SRic.Aleshire@Sun.COM return (EINVAL); 36110972SRic.Aleshire@Sun.COM 36210972SRic.Aleshire@Sun.COM /* 36310972SRic.Aleshire@Sun.COM * In a non-global zone, disallow attempts to set a label that 36410972SRic.Aleshire@Sun.COM * doesn't match that of the zone; otherwise no other checks 36510972SRic.Aleshire@Sun.COM * are needed. 36610972SRic.Aleshire@Sun.COM */ 36710972SRic.Aleshire@Sun.COM if (!INGLOBALZONE(curproc)) { 36810972SRic.Aleshire@Sun.COM if (new_default || !blequal(&new_sl, CR_SL(CRED()))) 36910972SRic.Aleshire@Sun.COM return (EPERM); 37010972SRic.Aleshire@Sun.COM return (0); 37110972SRic.Aleshire@Sun.COM } 37210972SRic.Aleshire@Sun.COM 37310972SRic.Aleshire@Sun.COM /* 37410972SRic.Aleshire@Sun.COM * For global-zone datasets (i.e., those whose zoned property is 37510972SRic.Aleshire@Sun.COM * "off", verify that the specified new label is valid for the 37610972SRic.Aleshire@Sun.COM * global zone. 37710972SRic.Aleshire@Sun.COM */ 37810972SRic.Aleshire@Sun.COM if (dsl_prop_get_integer(name, 37910972SRic.Aleshire@Sun.COM zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL)) 38010972SRic.Aleshire@Sun.COM return (EPERM); 38110972SRic.Aleshire@Sun.COM if (!zoned) { 38210972SRic.Aleshire@Sun.COM if (zfs_check_global_label(name, strval) != 0) 38310972SRic.Aleshire@Sun.COM return (EPERM); 38410972SRic.Aleshire@Sun.COM } 38510972SRic.Aleshire@Sun.COM 38610972SRic.Aleshire@Sun.COM /* 38710972SRic.Aleshire@Sun.COM * If the existing dataset label is nondefault, check if the 38810972SRic.Aleshire@Sun.COM * dataset is mounted (label cannot be changed while mounted). 38910972SRic.Aleshire@Sun.COM * Get the zfsvfs; if there isn't one, then the dataset isn't 39010972SRic.Aleshire@Sun.COM * mounted (or isn't a dataset, doesn't exist, ...). 39110972SRic.Aleshire@Sun.COM */ 39210972SRic.Aleshire@Sun.COM if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) { 39311022STom.Erickson@Sun.COM objset_t *os; 39411022STom.Erickson@Sun.COM static char *setsl_tag = "setsl_tag"; 39511022STom.Erickson@Sun.COM 39610972SRic.Aleshire@Sun.COM /* 39710972SRic.Aleshire@Sun.COM * Try to own the dataset; abort if there is any error, 39810972SRic.Aleshire@Sun.COM * (e.g., already mounted, in use, or other error). 39910972SRic.Aleshire@Sun.COM */ 40010972SRic.Aleshire@Sun.COM error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE, 40111022STom.Erickson@Sun.COM setsl_tag, &os); 40210972SRic.Aleshire@Sun.COM if (error) 40310972SRic.Aleshire@Sun.COM return (EPERM); 40410972SRic.Aleshire@Sun.COM 40511022STom.Erickson@Sun.COM dmu_objset_disown(os, setsl_tag); 40611022STom.Erickson@Sun.COM 40710972SRic.Aleshire@Sun.COM if (new_default) { 40810972SRic.Aleshire@Sun.COM needed_priv = PRIV_FILE_DOWNGRADE_SL; 40910972SRic.Aleshire@Sun.COM goto out_check; 41010972SRic.Aleshire@Sun.COM } 41110972SRic.Aleshire@Sun.COM 41210972SRic.Aleshire@Sun.COM if (hexstr_to_label(strval, &new_sl) != 0) 41310972SRic.Aleshire@Sun.COM return (EPERM); 41410972SRic.Aleshire@Sun.COM 41510972SRic.Aleshire@Sun.COM if (blstrictdom(&ds_sl, &new_sl)) 41610972SRic.Aleshire@Sun.COM needed_priv = PRIV_FILE_DOWNGRADE_SL; 41710972SRic.Aleshire@Sun.COM else if (blstrictdom(&new_sl, &ds_sl)) 41810972SRic.Aleshire@Sun.COM needed_priv = PRIV_FILE_UPGRADE_SL; 41910972SRic.Aleshire@Sun.COM } else { 42010972SRic.Aleshire@Sun.COM /* dataset currently has a default label */ 42110972SRic.Aleshire@Sun.COM if (!new_default) 42210972SRic.Aleshire@Sun.COM needed_priv = PRIV_FILE_UPGRADE_SL; 42310972SRic.Aleshire@Sun.COM } 42410972SRic.Aleshire@Sun.COM 42510972SRic.Aleshire@Sun.COM out_check: 42610972SRic.Aleshire@Sun.COM if (needed_priv != -1) 42710972SRic.Aleshire@Sun.COM return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL)); 42810972SRic.Aleshire@Sun.COM return (0); 42910972SRic.Aleshire@Sun.COM } 43010972SRic.Aleshire@Sun.COM 4314543Smarks static int 43211022STom.Erickson@Sun.COM zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval, 43311022STom.Erickson@Sun.COM cred_t *cr) 4344543Smarks { 43511022STom.Erickson@Sun.COM char *strval; 43611022STom.Erickson@Sun.COM 4374543Smarks /* 4384543Smarks * Check permissions for special properties. 4394543Smarks */ 4404543Smarks switch (prop) { 4414543Smarks case ZFS_PROP_ZONED: 4424543Smarks /* 4434543Smarks * Disallow setting of 'zoned' from within a local zone. 4444543Smarks */ 4454543Smarks if (!INGLOBALZONE(curproc)) 4464543Smarks return (EPERM); 4474543Smarks break; 448789Sahrens 4494543Smarks case ZFS_PROP_QUOTA: 4504543Smarks if (!INGLOBALZONE(curproc)) { 4514543Smarks uint64_t zoned; 4524543Smarks char setpoint[MAXNAMELEN]; 4534543Smarks /* 4544543Smarks * Unprivileged users are allowed to modify the 4554543Smarks * quota on things *under* (ie. contained by) 4564543Smarks * the thing they own. 4574543Smarks */ 45811022STom.Erickson@Sun.COM if (dsl_prop_get_integer(dsname, "zoned", &zoned, 4594543Smarks setpoint)) 4604543Smarks return (EPERM); 46111022STom.Erickson@Sun.COM if (!zoned || strlen(dsname) <= strlen(setpoint)) 4624543Smarks return (EPERM); 4634543Smarks } 4644670Sahrens break; 46510972SRic.Aleshire@Sun.COM 46610972SRic.Aleshire@Sun.COM case ZFS_PROP_MLSLABEL: 46710972SRic.Aleshire@Sun.COM if (!is_system_labeled()) 46810972SRic.Aleshire@Sun.COM return (EPERM); 46911022STom.Erickson@Sun.COM 47011022STom.Erickson@Sun.COM if (nvpair_value_string(propval, &strval) == 0) { 47111022STom.Erickson@Sun.COM int err; 47211022STom.Erickson@Sun.COM 47311022STom.Erickson@Sun.COM err = zfs_set_slabel_policy(dsname, strval, CRED()); 47411022STom.Erickson@Sun.COM if (err != 0) 47511022STom.Erickson@Sun.COM return (err); 47611022STom.Erickson@Sun.COM } 47710972SRic.Aleshire@Sun.COM break; 4784543Smarks } 4794543Smarks 48011022STom.Erickson@Sun.COM return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr)); 481789Sahrens } 482789Sahrens 4834543Smarks int 4844543Smarks zfs_secpolicy_fsacl(zfs_cmd_t *zc, cred_t *cr) 4854543Smarks { 4864543Smarks int error; 4874543Smarks 4884543Smarks error = zfs_dozonecheck(zc->zc_name, cr); 4894543Smarks if (error) 4904543Smarks return (error); 4914543Smarks 4924543Smarks /* 4934543Smarks * permission to set permissions will be evaluated later in 4944543Smarks * dsl_deleg_can_allow() 4954543Smarks */ 4964543Smarks return (0); 4974543Smarks } 4984543Smarks 4994543Smarks int 5004543Smarks zfs_secpolicy_rollback(zfs_cmd_t *zc, cred_t *cr) 5014543Smarks { 50210588SEric.Taylor@Sun.COM return (zfs_secpolicy_write_perms(zc->zc_name, 50310588SEric.Taylor@Sun.COM ZFS_DELEG_PERM_ROLLBACK, cr)); 5044543Smarks } 5054543Smarks 5064543Smarks int 5074543Smarks zfs_secpolicy_send(zfs_cmd_t *zc, cred_t *cr) 5084543Smarks { 5094543Smarks return (zfs_secpolicy_write_perms(zc->zc_name, 5104543Smarks ZFS_DELEG_PERM_SEND, cr)); 5114543Smarks } 5124543Smarks 5138845Samw@Sun.COM static int 5148845Samw@Sun.COM zfs_secpolicy_deleg_share(zfs_cmd_t *zc, cred_t *cr) 5158845Samw@Sun.COM { 5168845Samw@Sun.COM vnode_t *vp; 5178845Samw@Sun.COM int error; 5188845Samw@Sun.COM 5198845Samw@Sun.COM if ((error = lookupname(zc->zc_value, UIO_SYSSPACE, 5208845Samw@Sun.COM NO_FOLLOW, NULL, &vp)) != 0) 5218845Samw@Sun.COM return (error); 5228845Samw@Sun.COM 5238845Samw@Sun.COM /* Now make sure mntpnt and dataset are ZFS */ 5248845Samw@Sun.COM 5258845Samw@Sun.COM if (vp->v_vfsp->vfs_fstype != zfsfstype || 5268845Samw@Sun.COM (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource), 5278845Samw@Sun.COM zc->zc_name) != 0)) { 5288845Samw@Sun.COM VN_RELE(vp); 5298845Samw@Sun.COM return (EPERM); 5308845Samw@Sun.COM } 5318845Samw@Sun.COM 5328845Samw@Sun.COM VN_RELE(vp); 5338845Samw@Sun.COM return (dsl_deleg_access(zc->zc_name, 5348845Samw@Sun.COM ZFS_DELEG_PERM_SHARE, cr)); 5358845Samw@Sun.COM } 5368845Samw@Sun.COM 5374543Smarks int 5384543Smarks zfs_secpolicy_share(zfs_cmd_t *zc, cred_t *cr) 5394543Smarks { 5404543Smarks if (!INGLOBALZONE(curproc)) 5414543Smarks return (EPERM); 5424543Smarks 5435367Sahrens if (secpolicy_nfs(cr) == 0) { 5444543Smarks return (0); 5454543Smarks } else { 5468845Samw@Sun.COM return (zfs_secpolicy_deleg_share(zc, cr)); 5478845Samw@Sun.COM } 5488845Samw@Sun.COM } 5498845Samw@Sun.COM 5508845Samw@Sun.COM int 5518845Samw@Sun.COM zfs_secpolicy_smb_acl(zfs_cmd_t *zc, cred_t *cr) 5528845Samw@Sun.COM { 5538845Samw@Sun.COM if (!INGLOBALZONE(curproc)) 5548845Samw@Sun.COM return (EPERM); 5558845Samw@Sun.COM 5568845Samw@Sun.COM if (secpolicy_smb(cr) == 0) { 5578845Samw@Sun.COM return (0); 5588845Samw@Sun.COM } else { 5598845Samw@Sun.COM return (zfs_secpolicy_deleg_share(zc, cr)); 5604543Smarks } 5614543Smarks } 5624543Smarks 563789Sahrens static int 5644543Smarks zfs_get_parent(const char *datasetname, char *parent, int parentsize) 565789Sahrens { 566789Sahrens char *cp; 567789Sahrens 568789Sahrens /* 569789Sahrens * Remove the @bla or /bla from the end of the name to get the parent. 570789Sahrens */ 5714543Smarks (void) strncpy(parent, datasetname, parentsize); 5724543Smarks cp = strrchr(parent, '@'); 573789Sahrens if (cp != NULL) { 574789Sahrens cp[0] = '\0'; 575789Sahrens } else { 5764543Smarks cp = strrchr(parent, '/'); 577789Sahrens if (cp == NULL) 578789Sahrens return (ENOENT); 579789Sahrens cp[0] = '\0'; 580789Sahrens } 581789Sahrens 5824543Smarks return (0); 5834543Smarks } 5844543Smarks 5854543Smarks int 5864543Smarks zfs_secpolicy_destroy_perms(const char *name, cred_t *cr) 5874543Smarks { 5884543Smarks int error; 5894543Smarks 5904543Smarks if ((error = zfs_secpolicy_write_perms(name, 5914543Smarks ZFS_DELEG_PERM_MOUNT, cr)) != 0) 5924543Smarks return (error); 5934543Smarks 5944543Smarks return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr)); 5954543Smarks } 5964543Smarks 5974543Smarks static int 5984543Smarks zfs_secpolicy_destroy(zfs_cmd_t *zc, cred_t *cr) 5994543Smarks { 6004543Smarks return (zfs_secpolicy_destroy_perms(zc->zc_name, cr)); 6014543Smarks } 6024543Smarks 6034543Smarks /* 60411314Swilliam.gorrell@sun.com * Destroying snapshots with delegated permissions requires 60511314Swilliam.gorrell@sun.com * descendent mount and destroy permissions. 60611314Swilliam.gorrell@sun.com * Reassemble the full filesystem@snap name so dsl_deleg_access() 60711314Swilliam.gorrell@sun.com * can do the correct permission check. 60811314Swilliam.gorrell@sun.com * 60911314Swilliam.gorrell@sun.com * Since this routine is used when doing a recursive destroy of snapshots 61011314Swilliam.gorrell@sun.com * and destroying snapshots requires descendent permissions, a successfull 61111314Swilliam.gorrell@sun.com * check of the top level snapshot applies to snapshots of all descendent 61211314Swilliam.gorrell@sun.com * datasets as well. 61311314Swilliam.gorrell@sun.com */ 61411314Swilliam.gorrell@sun.com static int 61511314Swilliam.gorrell@sun.com zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, cred_t *cr) 61611314Swilliam.gorrell@sun.com { 61711314Swilliam.gorrell@sun.com int error; 61811314Swilliam.gorrell@sun.com char *dsname; 61911314Swilliam.gorrell@sun.com 62011314Swilliam.gorrell@sun.com dsname = kmem_asprintf("%s@%s", zc->zc_name, zc->zc_value); 62111314Swilliam.gorrell@sun.com 62211314Swilliam.gorrell@sun.com error = zfs_secpolicy_destroy_perms(dsname, cr); 62311314Swilliam.gorrell@sun.com 62411314Swilliam.gorrell@sun.com strfree(dsname); 62511314Swilliam.gorrell@sun.com return (error); 62611314Swilliam.gorrell@sun.com } 62711314Swilliam.gorrell@sun.com 6284543Smarks int 6294543Smarks zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr) 6304543Smarks { 63111022STom.Erickson@Sun.COM char parentname[MAXNAMELEN]; 6324543Smarks int error; 6334543Smarks 6344543Smarks if ((error = zfs_secpolicy_write_perms(from, 6354543Smarks ZFS_DELEG_PERM_RENAME, cr)) != 0) 6364543Smarks return (error); 6374543Smarks 6384543Smarks if ((error = zfs_secpolicy_write_perms(from, 6394543Smarks ZFS_DELEG_PERM_MOUNT, cr)) != 0) 6404543Smarks return (error); 6414543Smarks 6424543Smarks if ((error = zfs_get_parent(to, parentname, 6434543Smarks sizeof (parentname))) != 0) 6444543Smarks return (error); 6454543Smarks 6464543Smarks if ((error = zfs_secpolicy_write_perms(parentname, 6474543Smarks ZFS_DELEG_PERM_CREATE, cr)) != 0) 6484543Smarks return (error); 6494543Smarks 6504543Smarks if ((error = zfs_secpolicy_write_perms(parentname, 6514543Smarks ZFS_DELEG_PERM_MOUNT, cr)) != 0) 6524543Smarks return (error); 6534543Smarks 6544543Smarks return (error); 6554543Smarks } 6564543Smarks 6574543Smarks static int 6584543Smarks zfs_secpolicy_rename(zfs_cmd_t *zc, cred_t *cr) 6594543Smarks { 6604543Smarks return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr)); 6614543Smarks } 6624543Smarks 6634543Smarks static int 6644543Smarks zfs_secpolicy_promote(zfs_cmd_t *zc, cred_t *cr) 6654543Smarks { 66611022STom.Erickson@Sun.COM char parentname[MAXNAMELEN]; 6674543Smarks objset_t *clone; 6684543Smarks int error; 6694543Smarks 6704543Smarks error = zfs_secpolicy_write_perms(zc->zc_name, 6714543Smarks ZFS_DELEG_PERM_PROMOTE, cr); 6724543Smarks if (error) 6734543Smarks return (error); 6744543Smarks 67510298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(zc->zc_name, FTAG, &clone); 6764543Smarks 6774543Smarks if (error == 0) { 6784543Smarks dsl_dataset_t *pclone = NULL; 6794543Smarks dsl_dir_t *dd; 68010298SMatthew.Ahrens@Sun.COM dd = clone->os_dsl_dataset->ds_dir; 6814543Smarks 6824543Smarks rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER); 6836689Smaybee error = dsl_dataset_hold_obj(dd->dd_pool, 6846689Smaybee dd->dd_phys->dd_origin_obj, FTAG, &pclone); 6854543Smarks rw_exit(&dd->dd_pool->dp_config_rwlock); 6864543Smarks if (error) { 68710298SMatthew.Ahrens@Sun.COM dmu_objset_rele(clone, FTAG); 6884543Smarks return (error); 6894543Smarks } 6904543Smarks 6914543Smarks error = zfs_secpolicy_write_perms(zc->zc_name, 6924543Smarks ZFS_DELEG_PERM_MOUNT, cr); 6934543Smarks 6944543Smarks dsl_dataset_name(pclone, parentname); 69510298SMatthew.Ahrens@Sun.COM dmu_objset_rele(clone, FTAG); 6966689Smaybee dsl_dataset_rele(pclone, FTAG); 6974543Smarks if (error == 0) 6984543Smarks error = zfs_secpolicy_write_perms(parentname, 6994543Smarks ZFS_DELEG_PERM_PROMOTE, cr); 7004543Smarks } 7014543Smarks return (error); 7024543Smarks } 7034543Smarks 7044543Smarks static int 7054543Smarks zfs_secpolicy_receive(zfs_cmd_t *zc, cred_t *cr) 7064543Smarks { 7074543Smarks int error; 7084543Smarks 7094543Smarks if ((error = zfs_secpolicy_write_perms(zc->zc_name, 7104543Smarks ZFS_DELEG_PERM_RECEIVE, cr)) != 0) 7114543Smarks return (error); 7124543Smarks 7134543Smarks if ((error = zfs_secpolicy_write_perms(zc->zc_name, 7144543Smarks ZFS_DELEG_PERM_MOUNT, cr)) != 0) 7154543Smarks return (error); 7164543Smarks 7174543Smarks return (zfs_secpolicy_write_perms(zc->zc_name, 7184543Smarks ZFS_DELEG_PERM_CREATE, cr)); 7194543Smarks } 7204543Smarks 7214543Smarks int 7224543Smarks zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr) 7234543Smarks { 72410588SEric.Taylor@Sun.COM return (zfs_secpolicy_write_perms(name, 72510588SEric.Taylor@Sun.COM ZFS_DELEG_PERM_SNAPSHOT, cr)); 7264543Smarks } 7274543Smarks 7284543Smarks static int 7294543Smarks zfs_secpolicy_snapshot(zfs_cmd_t *zc, cred_t *cr) 7304543Smarks { 7314543Smarks 7324543Smarks return (zfs_secpolicy_snapshot_perms(zc->zc_name, cr)); 7334543Smarks } 7344543Smarks 7354543Smarks static int 7364543Smarks zfs_secpolicy_create(zfs_cmd_t *zc, cred_t *cr) 7374543Smarks { 73811022STom.Erickson@Sun.COM char parentname[MAXNAMELEN]; 73911022STom.Erickson@Sun.COM int error; 7404543Smarks 7414543Smarks if ((error = zfs_get_parent(zc->zc_name, parentname, 7424543Smarks sizeof (parentname))) != 0) 7434543Smarks return (error); 7444543Smarks 7454543Smarks if (zc->zc_value[0] != '\0') { 7464543Smarks if ((error = zfs_secpolicy_write_perms(zc->zc_value, 7474543Smarks ZFS_DELEG_PERM_CLONE, cr)) != 0) 7484543Smarks return (error); 7494543Smarks } 7504543Smarks 7514543Smarks if ((error = zfs_secpolicy_write_perms(parentname, 7524543Smarks ZFS_DELEG_PERM_CREATE, cr)) != 0) 7534543Smarks return (error); 7544543Smarks 7554543Smarks error = zfs_secpolicy_write_perms(parentname, 7564543Smarks ZFS_DELEG_PERM_MOUNT, cr); 7574543Smarks 7584543Smarks return (error); 7594543Smarks } 7604543Smarks 7614543Smarks static int 7624543Smarks zfs_secpolicy_umount(zfs_cmd_t *zc, cred_t *cr) 7634543Smarks { 7644543Smarks int error; 7654543Smarks 7664543Smarks error = secpolicy_fs_unmount(cr, NULL); 7674543Smarks if (error) { 7684543Smarks error = dsl_deleg_access(zc->zc_name, ZFS_DELEG_PERM_MOUNT, cr); 7694543Smarks } 7704543Smarks return (error); 771789Sahrens } 772789Sahrens 773789Sahrens /* 774789Sahrens * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires 775789Sahrens * SYS_CONFIG privilege, which is not available in a local zone. 776789Sahrens */ 777789Sahrens /* ARGSUSED */ 778789Sahrens static int 7794543Smarks zfs_secpolicy_config(zfs_cmd_t *zc, cred_t *cr) 780789Sahrens { 781789Sahrens if (secpolicy_sys_config(cr, B_FALSE) != 0) 782789Sahrens return (EPERM); 783789Sahrens 784789Sahrens return (0); 785789Sahrens } 786789Sahrens 787789Sahrens /* 7881544Seschrock * Policy for fault injection. Requires all privileges. 7891544Seschrock */ 7901544Seschrock /* ARGSUSED */ 7911544Seschrock static int 7924543Smarks zfs_secpolicy_inject(zfs_cmd_t *zc, cred_t *cr) 7931544Seschrock { 7941544Seschrock return (secpolicy_zinject(cr)); 7951544Seschrock } 7961544Seschrock 7974849Sahrens static int 7984849Sahrens zfs_secpolicy_inherit(zfs_cmd_t *zc, cred_t *cr) 7994849Sahrens { 8004849Sahrens zfs_prop_t prop = zfs_name_to_prop(zc->zc_value); 8014849Sahrens 8025094Slling if (prop == ZPROP_INVAL) { 8034849Sahrens if (!zfs_prop_user(zc->zc_value)) 8044849Sahrens return (EINVAL); 8054849Sahrens return (zfs_secpolicy_write_perms(zc->zc_name, 8064849Sahrens ZFS_DELEG_PERM_USERPROP, cr)); 8074849Sahrens } else { 80811022STom.Erickson@Sun.COM return (zfs_secpolicy_setprop(zc->zc_name, prop, 80911022STom.Erickson@Sun.COM NULL, cr)); 8104849Sahrens } 8114849Sahrens } 8124849Sahrens 8139396SMatthew.Ahrens@Sun.COM static int 8149396SMatthew.Ahrens@Sun.COM zfs_secpolicy_userspace_one(zfs_cmd_t *zc, cred_t *cr) 8159396SMatthew.Ahrens@Sun.COM { 8169396SMatthew.Ahrens@Sun.COM int err = zfs_secpolicy_read(zc, cr); 8179396SMatthew.Ahrens@Sun.COM if (err) 8189396SMatthew.Ahrens@Sun.COM return (err); 8199396SMatthew.Ahrens@Sun.COM 8209396SMatthew.Ahrens@Sun.COM if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS) 8219396SMatthew.Ahrens@Sun.COM return (EINVAL); 8229396SMatthew.Ahrens@Sun.COM 8239396SMatthew.Ahrens@Sun.COM if (zc->zc_value[0] == 0) { 8249396SMatthew.Ahrens@Sun.COM /* 8259396SMatthew.Ahrens@Sun.COM * They are asking about a posix uid/gid. If it's 8269396SMatthew.Ahrens@Sun.COM * themself, allow it. 8279396SMatthew.Ahrens@Sun.COM */ 8289396SMatthew.Ahrens@Sun.COM if (zc->zc_objset_type == ZFS_PROP_USERUSED || 8299396SMatthew.Ahrens@Sun.COM zc->zc_objset_type == ZFS_PROP_USERQUOTA) { 8309396SMatthew.Ahrens@Sun.COM if (zc->zc_guid == crgetuid(cr)) 8319396SMatthew.Ahrens@Sun.COM return (0); 8329396SMatthew.Ahrens@Sun.COM } else { 8339396SMatthew.Ahrens@Sun.COM if (groupmember(zc->zc_guid, cr)) 8349396SMatthew.Ahrens@Sun.COM return (0); 8359396SMatthew.Ahrens@Sun.COM } 8369396SMatthew.Ahrens@Sun.COM } 8379396SMatthew.Ahrens@Sun.COM 8389396SMatthew.Ahrens@Sun.COM return (zfs_secpolicy_write_perms(zc->zc_name, 8399396SMatthew.Ahrens@Sun.COM userquota_perms[zc->zc_objset_type], cr)); 8409396SMatthew.Ahrens@Sun.COM } 8419396SMatthew.Ahrens@Sun.COM 8429396SMatthew.Ahrens@Sun.COM static int 8439396SMatthew.Ahrens@Sun.COM zfs_secpolicy_userspace_many(zfs_cmd_t *zc, cred_t *cr) 8449396SMatthew.Ahrens@Sun.COM { 8459396SMatthew.Ahrens@Sun.COM int err = zfs_secpolicy_read(zc, cr); 8469396SMatthew.Ahrens@Sun.COM if (err) 8479396SMatthew.Ahrens@Sun.COM return (err); 8489396SMatthew.Ahrens@Sun.COM 8499396SMatthew.Ahrens@Sun.COM if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS) 8509396SMatthew.Ahrens@Sun.COM return (EINVAL); 8519396SMatthew.Ahrens@Sun.COM 8529396SMatthew.Ahrens@Sun.COM return (zfs_secpolicy_write_perms(zc->zc_name, 8539396SMatthew.Ahrens@Sun.COM userquota_perms[zc->zc_objset_type], cr)); 8549396SMatthew.Ahrens@Sun.COM } 8559396SMatthew.Ahrens@Sun.COM 8569396SMatthew.Ahrens@Sun.COM static int 8579396SMatthew.Ahrens@Sun.COM zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, cred_t *cr) 8589396SMatthew.Ahrens@Sun.COM { 85911022STom.Erickson@Sun.COM return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION, 86011022STom.Erickson@Sun.COM NULL, cr)); 8619396SMatthew.Ahrens@Sun.COM } 8629396SMatthew.Ahrens@Sun.COM 86310242Schris.kirby@sun.com static int 86410242Schris.kirby@sun.com zfs_secpolicy_hold(zfs_cmd_t *zc, cred_t *cr) 86510242Schris.kirby@sun.com { 86610242Schris.kirby@sun.com return (zfs_secpolicy_write_perms(zc->zc_name, 86710242Schris.kirby@sun.com ZFS_DELEG_PERM_HOLD, cr)); 86810242Schris.kirby@sun.com } 86910242Schris.kirby@sun.com 87010242Schris.kirby@sun.com static int 87110242Schris.kirby@sun.com zfs_secpolicy_release(zfs_cmd_t *zc, cred_t *cr) 87210242Schris.kirby@sun.com { 87310242Schris.kirby@sun.com return (zfs_secpolicy_write_perms(zc->zc_name, 87410242Schris.kirby@sun.com ZFS_DELEG_PERM_RELEASE, cr)); 87510242Schris.kirby@sun.com } 87610242Schris.kirby@sun.com 8771544Seschrock /* 878789Sahrens * Returns the nvlist as specified by the user in the zfs_cmd_t. 879789Sahrens */ 880789Sahrens static int 8819643SEric.Taylor@Sun.COM get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp) 882789Sahrens { 883789Sahrens char *packed; 884789Sahrens int error; 8855094Slling nvlist_t *list = NULL; 886789Sahrens 887789Sahrens /* 8882676Seschrock * Read in and unpack the user-supplied nvlist. 889789Sahrens */ 8905094Slling if (size == 0) 891789Sahrens return (EINVAL); 892789Sahrens 893789Sahrens packed = kmem_alloc(size, KM_SLEEP); 894789Sahrens 8959643SEric.Taylor@Sun.COM if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size, 8969643SEric.Taylor@Sun.COM iflag)) != 0) { 897789Sahrens kmem_free(packed, size); 898789Sahrens return (error); 899789Sahrens } 900789Sahrens 9015094Slling if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) { 902789Sahrens kmem_free(packed, size); 903789Sahrens return (error); 904789Sahrens } 905789Sahrens 906789Sahrens kmem_free(packed, size); 907789Sahrens 9085094Slling *nvp = list; 909789Sahrens return (0); 910789Sahrens } 911789Sahrens 912789Sahrens static int 91311022STom.Erickson@Sun.COM fit_error_list(zfs_cmd_t *zc, nvlist_t **errors) 91411022STom.Erickson@Sun.COM { 91511022STom.Erickson@Sun.COM size_t size; 91611022STom.Erickson@Sun.COM 91711022STom.Erickson@Sun.COM VERIFY(nvlist_size(*errors, &size, NV_ENCODE_NATIVE) == 0); 91811022STom.Erickson@Sun.COM 91911022STom.Erickson@Sun.COM if (size > zc->zc_nvlist_dst_size) { 92011022STom.Erickson@Sun.COM nvpair_t *more_errors; 92111022STom.Erickson@Sun.COM int n = 0; 92211022STom.Erickson@Sun.COM 92311022STom.Erickson@Sun.COM if (zc->zc_nvlist_dst_size < 1024) 92411022STom.Erickson@Sun.COM return (ENOMEM); 92511022STom.Erickson@Sun.COM 92611022STom.Erickson@Sun.COM VERIFY(nvlist_add_int32(*errors, ZPROP_N_MORE_ERRORS, 0) == 0); 92711022STom.Erickson@Sun.COM more_errors = nvlist_prev_nvpair(*errors, NULL); 92811022STom.Erickson@Sun.COM 92911022STom.Erickson@Sun.COM do { 93011022STom.Erickson@Sun.COM nvpair_t *pair = nvlist_prev_nvpair(*errors, 93111022STom.Erickson@Sun.COM more_errors); 93211022STom.Erickson@Sun.COM VERIFY(nvlist_remove_nvpair(*errors, pair) == 0); 93311022STom.Erickson@Sun.COM n++; 93411022STom.Erickson@Sun.COM VERIFY(nvlist_size(*errors, &size, 93511022STom.Erickson@Sun.COM NV_ENCODE_NATIVE) == 0); 93611022STom.Erickson@Sun.COM } while (size > zc->zc_nvlist_dst_size); 93711022STom.Erickson@Sun.COM 93811022STom.Erickson@Sun.COM VERIFY(nvlist_remove_nvpair(*errors, more_errors) == 0); 93911022STom.Erickson@Sun.COM VERIFY(nvlist_add_int32(*errors, ZPROP_N_MORE_ERRORS, n) == 0); 94011022STom.Erickson@Sun.COM ASSERT(nvlist_size(*errors, &size, NV_ENCODE_NATIVE) == 0); 94111022STom.Erickson@Sun.COM ASSERT(size <= zc->zc_nvlist_dst_size); 94211022STom.Erickson@Sun.COM } 94311022STom.Erickson@Sun.COM 94411022STom.Erickson@Sun.COM return (0); 94511022STom.Erickson@Sun.COM } 94611022STom.Erickson@Sun.COM 94711022STom.Erickson@Sun.COM static int 9482676Seschrock put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl) 9492676Seschrock { 9502676Seschrock char *packed = NULL; 95111807SSam.Falkner@Sun.COM int error = 0; 9522676Seschrock size_t size; 9532676Seschrock 9542676Seschrock VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0); 9552676Seschrock 9562676Seschrock if (size > zc->zc_nvlist_dst_size) { 9572676Seschrock error = ENOMEM; 9582676Seschrock } else { 9594611Smarks packed = kmem_alloc(size, KM_SLEEP); 9602676Seschrock VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE, 9612676Seschrock KM_SLEEP) == 0); 96211807SSam.Falkner@Sun.COM if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst, 96311807SSam.Falkner@Sun.COM size, zc->zc_iflags) != 0) 96411807SSam.Falkner@Sun.COM error = EFAULT; 9652676Seschrock kmem_free(packed, size); 9662676Seschrock } 9672676Seschrock 9682676Seschrock zc->zc_nvlist_dst_size = size; 9692676Seschrock return (error); 9702676Seschrock } 9712676Seschrock 9722676Seschrock static int 97311185SSean.McEnroe@Sun.COM getzfsvfs(const char *dsname, zfsvfs_t **zfvp) 9749396SMatthew.Ahrens@Sun.COM { 9759396SMatthew.Ahrens@Sun.COM objset_t *os; 9769396SMatthew.Ahrens@Sun.COM int error; 9779396SMatthew.Ahrens@Sun.COM 97810298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(dsname, FTAG, &os); 9799396SMatthew.Ahrens@Sun.COM if (error) 9809396SMatthew.Ahrens@Sun.COM return (error); 98110298SMatthew.Ahrens@Sun.COM if (dmu_objset_type(os) != DMU_OST_ZFS) { 98210298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 98310298SMatthew.Ahrens@Sun.COM return (EINVAL); 98410298SMatthew.Ahrens@Sun.COM } 98510298SMatthew.Ahrens@Sun.COM 98610298SMatthew.Ahrens@Sun.COM mutex_enter(&os->os_user_ptr_lock); 98711185SSean.McEnroe@Sun.COM *zfvp = dmu_objset_get_user(os); 98811185SSean.McEnroe@Sun.COM if (*zfvp) { 98911185SSean.McEnroe@Sun.COM VFS_HOLD((*zfvp)->z_vfs); 9909396SMatthew.Ahrens@Sun.COM } else { 9919396SMatthew.Ahrens@Sun.COM error = ESRCH; 9929396SMatthew.Ahrens@Sun.COM } 99310298SMatthew.Ahrens@Sun.COM mutex_exit(&os->os_user_ptr_lock); 99410298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 9959396SMatthew.Ahrens@Sun.COM return (error); 9969396SMatthew.Ahrens@Sun.COM } 9979396SMatthew.Ahrens@Sun.COM 9989396SMatthew.Ahrens@Sun.COM /* 9999396SMatthew.Ahrens@Sun.COM * Find a zfsvfs_t for a mounted filesystem, or create our own, in which 10009396SMatthew.Ahrens@Sun.COM * case its z_vfs will be NULL, and it will be opened as the owner. 10019396SMatthew.Ahrens@Sun.COM */ 10029396SMatthew.Ahrens@Sun.COM static int 100311185SSean.McEnroe@Sun.COM zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp) 10049396SMatthew.Ahrens@Sun.COM { 10059396SMatthew.Ahrens@Sun.COM int error = 0; 10069396SMatthew.Ahrens@Sun.COM 100711185SSean.McEnroe@Sun.COM if (getzfsvfs(name, zfvp) != 0) 100811185SSean.McEnroe@Sun.COM error = zfsvfs_create(name, zfvp); 10099396SMatthew.Ahrens@Sun.COM if (error == 0) { 101011185SSean.McEnroe@Sun.COM rrw_enter(&(*zfvp)->z_teardown_lock, RW_READER, tag); 101111185SSean.McEnroe@Sun.COM if ((*zfvp)->z_unmounted) { 10129396SMatthew.Ahrens@Sun.COM /* 10139396SMatthew.Ahrens@Sun.COM * XXX we could probably try again, since the unmounting 10149396SMatthew.Ahrens@Sun.COM * thread should be just about to disassociate the 10159396SMatthew.Ahrens@Sun.COM * objset from the zfsvfs. 10169396SMatthew.Ahrens@Sun.COM */ 101711185SSean.McEnroe@Sun.COM rrw_exit(&(*zfvp)->z_teardown_lock, tag); 10189396SMatthew.Ahrens@Sun.COM return (EBUSY); 10199396SMatthew.Ahrens@Sun.COM } 10209396SMatthew.Ahrens@Sun.COM } 10219396SMatthew.Ahrens@Sun.COM return (error); 10229396SMatthew.Ahrens@Sun.COM } 10239396SMatthew.Ahrens@Sun.COM 10249396SMatthew.Ahrens@Sun.COM static void 10259396SMatthew.Ahrens@Sun.COM zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag) 10269396SMatthew.Ahrens@Sun.COM { 10279396SMatthew.Ahrens@Sun.COM rrw_exit(&zfsvfs->z_teardown_lock, tag); 10289396SMatthew.Ahrens@Sun.COM 10299396SMatthew.Ahrens@Sun.COM if (zfsvfs->z_vfs) { 10309396SMatthew.Ahrens@Sun.COM VFS_RELE(zfsvfs->z_vfs); 10319396SMatthew.Ahrens@Sun.COM } else { 103210298SMatthew.Ahrens@Sun.COM dmu_objset_disown(zfsvfs->z_os, zfsvfs); 10339396SMatthew.Ahrens@Sun.COM zfsvfs_free(zfsvfs); 10349396SMatthew.Ahrens@Sun.COM } 10359396SMatthew.Ahrens@Sun.COM } 10369396SMatthew.Ahrens@Sun.COM 10379396SMatthew.Ahrens@Sun.COM static int 1038789Sahrens zfs_ioc_pool_create(zfs_cmd_t *zc) 1039789Sahrens { 1040789Sahrens int error; 10415094Slling nvlist_t *config, *props = NULL; 10427184Stimh nvlist_t *rootprops = NULL; 10437184Stimh nvlist_t *zplprops = NULL; 10444715Sek110237 char *buf; 1045789Sahrens 10465094Slling if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 10479643SEric.Taylor@Sun.COM zc->zc_iflags, &config)) 10484988Sek110237 return (error); 10494715Sek110237 10505094Slling if (zc->zc_nvlist_src_size != 0 && (error = 10519643SEric.Taylor@Sun.COM get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 10529643SEric.Taylor@Sun.COM zc->zc_iflags, &props))) { 10535094Slling nvlist_free(config); 10545094Slling return (error); 10555094Slling } 10565094Slling 10577184Stimh if (props) { 10587184Stimh nvlist_t *nvl = NULL; 10597184Stimh uint64_t version = SPA_VERSION; 10607184Stimh 10617184Stimh (void) nvlist_lookup_uint64(props, 10627184Stimh zpool_prop_to_name(ZPOOL_PROP_VERSION), &version); 10637184Stimh if (version < SPA_VERSION_INITIAL || version > SPA_VERSION) { 10647184Stimh error = EINVAL; 10657184Stimh goto pool_props_bad; 10667184Stimh } 10677184Stimh (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl); 10687184Stimh if (nvl) { 10697184Stimh error = nvlist_dup(nvl, &rootprops, KM_SLEEP); 10707184Stimh if (error != 0) { 10717184Stimh nvlist_free(config); 10727184Stimh nvlist_free(props); 10737184Stimh return (error); 10747184Stimh } 10757184Stimh (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS); 10767184Stimh } 10777184Stimh VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0); 10787184Stimh error = zfs_fill_zplprops_root(version, rootprops, 10797184Stimh zplprops, NULL); 10807184Stimh if (error) 10817184Stimh goto pool_props_bad; 10827184Stimh } 10837184Stimh 10844988Sek110237 buf = history_str_get(zc); 1085789Sahrens 10867184Stimh error = spa_create(zc->zc_name, config, props, buf, zplprops); 10877184Stimh 10887184Stimh /* 10897184Stimh * Set the remaining root properties 10907184Stimh */ 109111022STom.Erickson@Sun.COM if (!error && (error = zfs_set_prop_nvlist(zc->zc_name, 109211022STom.Erickson@Sun.COM ZPROP_SRC_LOCAL, rootprops, NULL)) != 0) 10937184Stimh (void) spa_destroy(zc->zc_name); 1094789Sahrens 10954988Sek110237 if (buf != NULL) 10964988Sek110237 history_str_free(buf); 10975094Slling 10987184Stimh pool_props_bad: 10997184Stimh nvlist_free(rootprops); 11007184Stimh nvlist_free(zplprops); 1101789Sahrens nvlist_free(config); 11027184Stimh nvlist_free(props); 11035094Slling 1104789Sahrens return (error); 1105789Sahrens } 1106789Sahrens 1107789Sahrens static int 1108789Sahrens zfs_ioc_pool_destroy(zfs_cmd_t *zc) 1109789Sahrens { 11104543Smarks int error; 11114543Smarks zfs_log_history(zc); 11124543Smarks error = spa_destroy(zc->zc_name); 111310588SEric.Taylor@Sun.COM if (error == 0) 111410588SEric.Taylor@Sun.COM zvol_remove_minors(zc->zc_name); 11154543Smarks return (error); 1116789Sahrens } 1117789Sahrens 1118789Sahrens static int 1119789Sahrens zfs_ioc_pool_import(zfs_cmd_t *zc) 1120789Sahrens { 11215094Slling nvlist_t *config, *props = NULL; 1122789Sahrens uint64_t guid; 112310921STim.Haley@Sun.COM int error; 1124789Sahrens 11255094Slling if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 11269643SEric.Taylor@Sun.COM zc->zc_iflags, &config)) != 0) 1127789Sahrens return (error); 1128789Sahrens 11295094Slling if (zc->zc_nvlist_src_size != 0 && (error = 11309643SEric.Taylor@Sun.COM get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 11319643SEric.Taylor@Sun.COM zc->zc_iflags, &props))) { 11325094Slling nvlist_free(config); 11335094Slling return (error); 11345094Slling } 11355094Slling 1136789Sahrens if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 || 11371544Seschrock guid != zc->zc_guid) 1138789Sahrens error = EINVAL; 11396643Seschrock else if (zc->zc_cookie) 114010921STim.Haley@Sun.COM error = spa_import_verbatim(zc->zc_name, config, props); 1141789Sahrens else 11425094Slling error = spa_import(zc->zc_name, config, props); 1143789Sahrens 114410921STim.Haley@Sun.COM if (zc->zc_nvlist_dst != 0) 114510921STim.Haley@Sun.COM (void) put_nvlist(zc, config); 114610921STim.Haley@Sun.COM 1147789Sahrens nvlist_free(config); 1148789Sahrens 11495094Slling if (props) 11505094Slling nvlist_free(props); 11515094Slling 1152789Sahrens return (error); 1153789Sahrens } 1154789Sahrens 1155789Sahrens static int 1156789Sahrens zfs_ioc_pool_export(zfs_cmd_t *zc) 1157789Sahrens { 11584543Smarks int error; 11597214Slling boolean_t force = (boolean_t)zc->zc_cookie; 11608211SGeorge.Wilson@Sun.COM boolean_t hardforce = (boolean_t)zc->zc_guid; 11617214Slling 11624543Smarks zfs_log_history(zc); 11638211SGeorge.Wilson@Sun.COM error = spa_export(zc->zc_name, NULL, force, hardforce); 116410588SEric.Taylor@Sun.COM if (error == 0) 116510588SEric.Taylor@Sun.COM zvol_remove_minors(zc->zc_name); 11664543Smarks return (error); 1167789Sahrens } 1168789Sahrens 1169789Sahrens static int 1170789Sahrens zfs_ioc_pool_configs(zfs_cmd_t *zc) 1171789Sahrens { 1172789Sahrens nvlist_t *configs; 1173789Sahrens int error; 1174789Sahrens 1175789Sahrens if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL) 1176789Sahrens return (EEXIST); 1177789Sahrens 11782676Seschrock error = put_nvlist(zc, configs); 1179789Sahrens 1180789Sahrens nvlist_free(configs); 1181789Sahrens 1182789Sahrens return (error); 1183789Sahrens } 1184789Sahrens 1185789Sahrens static int 1186789Sahrens zfs_ioc_pool_stats(zfs_cmd_t *zc) 1187789Sahrens { 1188789Sahrens nvlist_t *config; 1189789Sahrens int error; 11901544Seschrock int ret = 0; 1191789Sahrens 11922676Seschrock error = spa_get_stats(zc->zc_name, &config, zc->zc_value, 11932676Seschrock sizeof (zc->zc_value)); 1194789Sahrens 1195789Sahrens if (config != NULL) { 11962676Seschrock ret = put_nvlist(zc, config); 1197789Sahrens nvlist_free(config); 11981544Seschrock 11991544Seschrock /* 12001544Seschrock * The config may be present even if 'error' is non-zero. 12011544Seschrock * In this case we return success, and preserve the real errno 12021544Seschrock * in 'zc_cookie'. 12031544Seschrock */ 12041544Seschrock zc->zc_cookie = error; 1205789Sahrens } else { 12061544Seschrock ret = error; 1207789Sahrens } 1208789Sahrens 12091544Seschrock return (ret); 1210789Sahrens } 1211789Sahrens 1212789Sahrens /* 1213789Sahrens * Try to import the given pool, returning pool stats as appropriate so that 1214789Sahrens * user land knows which devices are available and overall pool health. 1215789Sahrens */ 1216789Sahrens static int 1217789Sahrens zfs_ioc_pool_tryimport(zfs_cmd_t *zc) 1218789Sahrens { 1219789Sahrens nvlist_t *tryconfig, *config; 1220789Sahrens int error; 1221789Sahrens 12225094Slling if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 12239643SEric.Taylor@Sun.COM zc->zc_iflags, &tryconfig)) != 0) 1224789Sahrens return (error); 1225789Sahrens 1226789Sahrens config = spa_tryimport(tryconfig); 1227789Sahrens 1228789Sahrens nvlist_free(tryconfig); 1229789Sahrens 1230789Sahrens if (config == NULL) 1231789Sahrens return (EINVAL); 1232789Sahrens 12332676Seschrock error = put_nvlist(zc, config); 1234789Sahrens nvlist_free(config); 1235789Sahrens 1236789Sahrens return (error); 1237789Sahrens } 1238789Sahrens 1239789Sahrens static int 1240789Sahrens zfs_ioc_pool_scrub(zfs_cmd_t *zc) 1241789Sahrens { 1242789Sahrens spa_t *spa; 1243789Sahrens int error; 1244789Sahrens 12452926Sek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 12462926Sek110237 return (error); 12472926Sek110237 12487046Sahrens error = spa_scrub(spa, zc->zc_cookie); 12492926Sek110237 12502926Sek110237 spa_close(spa, FTAG); 12512926Sek110237 1252789Sahrens return (error); 1253789Sahrens } 1254789Sahrens 1255789Sahrens static int 1256789Sahrens zfs_ioc_pool_freeze(zfs_cmd_t *zc) 1257789Sahrens { 1258789Sahrens spa_t *spa; 1259789Sahrens int error; 1260789Sahrens 1261789Sahrens error = spa_open(zc->zc_name, &spa, FTAG); 1262789Sahrens if (error == 0) { 1263789Sahrens spa_freeze(spa); 1264789Sahrens spa_close(spa, FTAG); 1265789Sahrens } 1266789Sahrens return (error); 1267789Sahrens } 1268789Sahrens 1269789Sahrens static int 12701760Seschrock zfs_ioc_pool_upgrade(zfs_cmd_t *zc) 12711760Seschrock { 12721760Seschrock spa_t *spa; 12731760Seschrock int error; 12741760Seschrock 12752926Sek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 12762926Sek110237 return (error); 12772926Sek110237 12785118Slling if (zc->zc_cookie < spa_version(spa) || zc->zc_cookie > SPA_VERSION) { 12795118Slling spa_close(spa, FTAG); 12805118Slling return (EINVAL); 12815118Slling } 12825118Slling 12835094Slling spa_upgrade(spa, zc->zc_cookie); 12842926Sek110237 spa_close(spa, FTAG); 12852926Sek110237 12862926Sek110237 return (error); 12872926Sek110237 } 12882926Sek110237 12892926Sek110237 static int 12902926Sek110237 zfs_ioc_pool_get_history(zfs_cmd_t *zc) 12912926Sek110237 { 12922926Sek110237 spa_t *spa; 12932926Sek110237 char *hist_buf; 12942926Sek110237 uint64_t size; 12952926Sek110237 int error; 12962926Sek110237 12972926Sek110237 if ((size = zc->zc_history_len) == 0) 12982926Sek110237 return (EINVAL); 12992926Sek110237 13002926Sek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 13012926Sek110237 return (error); 13022926Sek110237 13034577Sahrens if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) { 13043863Sek110237 spa_close(spa, FTAG); 13053863Sek110237 return (ENOTSUP); 13063863Sek110237 } 13073863Sek110237 13082926Sek110237 hist_buf = kmem_alloc(size, KM_SLEEP); 13092926Sek110237 if ((error = spa_history_get(spa, &zc->zc_history_offset, 13102926Sek110237 &zc->zc_history_len, hist_buf)) == 0) { 13119643SEric.Taylor@Sun.COM error = ddi_copyout(hist_buf, 13129643SEric.Taylor@Sun.COM (void *)(uintptr_t)zc->zc_history, 13139643SEric.Taylor@Sun.COM zc->zc_history_len, zc->zc_iflags); 13142926Sek110237 } 13152926Sek110237 13162926Sek110237 spa_close(spa, FTAG); 13172926Sek110237 kmem_free(hist_buf, size); 13182926Sek110237 return (error); 13192926Sek110237 } 13202926Sek110237 13212926Sek110237 static int 13223444Sek110237 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc) 13233444Sek110237 { 13243444Sek110237 int error; 13253444Sek110237 13263912Slling if (error = dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value)) 13273444Sek110237 return (error); 13283444Sek110237 13293444Sek110237 return (0); 13303444Sek110237 } 13313444Sek110237 133210298SMatthew.Ahrens@Sun.COM /* 133310298SMatthew.Ahrens@Sun.COM * inputs: 133410298SMatthew.Ahrens@Sun.COM * zc_name name of filesystem 133510298SMatthew.Ahrens@Sun.COM * zc_obj object to find 133610298SMatthew.Ahrens@Sun.COM * 133710298SMatthew.Ahrens@Sun.COM * outputs: 133810298SMatthew.Ahrens@Sun.COM * zc_value name of object 133910298SMatthew.Ahrens@Sun.COM */ 13403444Sek110237 static int 13413444Sek110237 zfs_ioc_obj_to_path(zfs_cmd_t *zc) 13423444Sek110237 { 134310298SMatthew.Ahrens@Sun.COM objset_t *os; 13443444Sek110237 int error; 13453444Sek110237 134610298SMatthew.Ahrens@Sun.COM /* XXX reading from objset not owned */ 134710298SMatthew.Ahrens@Sun.COM if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0) 13483444Sek110237 return (error); 134910298SMatthew.Ahrens@Sun.COM if (dmu_objset_type(os) != DMU_OST_ZFS) { 135010298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 135110298SMatthew.Ahrens@Sun.COM return (EINVAL); 135210298SMatthew.Ahrens@Sun.COM } 135310298SMatthew.Ahrens@Sun.COM error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value, 13543444Sek110237 sizeof (zc->zc_value)); 135510298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 13563444Sek110237 13573444Sek110237 return (error); 13583444Sek110237 } 13593444Sek110237 13603444Sek110237 static int 1361789Sahrens zfs_ioc_vdev_add(zfs_cmd_t *zc) 1362789Sahrens { 1363789Sahrens spa_t *spa; 1364789Sahrens int error; 13656423Sgw25295 nvlist_t *config, **l2cache, **spares; 13666423Sgw25295 uint_t nl2cache = 0, nspares = 0; 1367789Sahrens 1368789Sahrens error = spa_open(zc->zc_name, &spa, FTAG); 1369789Sahrens if (error != 0) 1370789Sahrens return (error); 1371789Sahrens 13725450Sbrendan error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 13739643SEric.Taylor@Sun.COM zc->zc_iflags, &config); 13745450Sbrendan (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE, 13755450Sbrendan &l2cache, &nl2cache); 13765450Sbrendan 13776423Sgw25295 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES, 13786423Sgw25295 &spares, &nspares); 13796423Sgw25295 13803912Slling /* 13813912Slling * A root pool with concatenated devices is not supported. 13826423Sgw25295 * Thus, can not add a device to a root pool. 13836423Sgw25295 * 13846423Sgw25295 * Intent log device can not be added to a rootpool because 13856423Sgw25295 * during mountroot, zil is replayed, a seperated log device 13866423Sgw25295 * can not be accessed during the mountroot time. 13876423Sgw25295 * 13886423Sgw25295 * l2cache and spare devices are ok to be added to a rootpool. 13893912Slling */ 139010922SJeff.Bonwick@Sun.COM if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) { 139111422SMark.Musante@Sun.COM nvlist_free(config); 13923912Slling spa_close(spa, FTAG); 13933912Slling return (EDOM); 13943912Slling } 13953912Slling 13965450Sbrendan if (error == 0) { 1397789Sahrens error = spa_vdev_add(spa, config); 1398789Sahrens nvlist_free(config); 1399789Sahrens } 1400789Sahrens spa_close(spa, FTAG); 1401789Sahrens return (error); 1402789Sahrens } 1403789Sahrens 1404789Sahrens static int 1405789Sahrens zfs_ioc_vdev_remove(zfs_cmd_t *zc) 1406789Sahrens { 14072082Seschrock spa_t *spa; 14082082Seschrock int error; 14092082Seschrock 14102082Seschrock error = spa_open(zc->zc_name, &spa, FTAG); 14112082Seschrock if (error != 0) 14122082Seschrock return (error); 14132082Seschrock error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE); 14142082Seschrock spa_close(spa, FTAG); 14152082Seschrock return (error); 1416789Sahrens } 1417789Sahrens 1418789Sahrens static int 14194451Seschrock zfs_ioc_vdev_set_state(zfs_cmd_t *zc) 1420789Sahrens { 1421789Sahrens spa_t *spa; 1422789Sahrens int error; 14234451Seschrock vdev_state_t newstate = VDEV_STATE_UNKNOWN; 1424789Sahrens 14252926Sek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1426789Sahrens return (error); 14274451Seschrock switch (zc->zc_cookie) { 14284451Seschrock case VDEV_STATE_ONLINE: 14294451Seschrock error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate); 14304451Seschrock break; 14314451Seschrock 14324451Seschrock case VDEV_STATE_OFFLINE: 14334451Seschrock error = vdev_offline(spa, zc->zc_guid, zc->zc_obj); 14344451Seschrock break; 1435789Sahrens 14364451Seschrock case VDEV_STATE_FAULTED: 143710817SEric.Schrock@Sun.COM if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED && 143810817SEric.Schrock@Sun.COM zc->zc_obj != VDEV_AUX_EXTERNAL) 143910817SEric.Schrock@Sun.COM zc->zc_obj = VDEV_AUX_ERR_EXCEEDED; 144010817SEric.Schrock@Sun.COM 144110817SEric.Schrock@Sun.COM error = vdev_fault(spa, zc->zc_guid, zc->zc_obj); 14424451Seschrock break; 1443789Sahrens 14444451Seschrock case VDEV_STATE_DEGRADED: 144510817SEric.Schrock@Sun.COM if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED && 144610817SEric.Schrock@Sun.COM zc->zc_obj != VDEV_AUX_EXTERNAL) 144710817SEric.Schrock@Sun.COM zc->zc_obj = VDEV_AUX_ERR_EXCEEDED; 144810817SEric.Schrock@Sun.COM 144910817SEric.Schrock@Sun.COM error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj); 14504451Seschrock break; 14514451Seschrock 14524451Seschrock default: 14534451Seschrock error = EINVAL; 14544451Seschrock } 14554451Seschrock zc->zc_cookie = newstate; 1456789Sahrens spa_close(spa, FTAG); 1457789Sahrens return (error); 1458789Sahrens } 1459789Sahrens 1460789Sahrens static int 1461789Sahrens zfs_ioc_vdev_attach(zfs_cmd_t *zc) 1462789Sahrens { 1463789Sahrens spa_t *spa; 1464789Sahrens int replacing = zc->zc_cookie; 1465789Sahrens nvlist_t *config; 1466789Sahrens int error; 1467789Sahrens 14682926Sek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1469789Sahrens return (error); 1470789Sahrens 14715094Slling if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 14729643SEric.Taylor@Sun.COM zc->zc_iflags, &config)) == 0) { 14731544Seschrock error = spa_vdev_attach(spa, zc->zc_guid, config, replacing); 1474789Sahrens nvlist_free(config); 1475789Sahrens } 1476789Sahrens 1477789Sahrens spa_close(spa, FTAG); 1478789Sahrens return (error); 1479789Sahrens } 1480789Sahrens 1481789Sahrens static int 1482789Sahrens zfs_ioc_vdev_detach(zfs_cmd_t *zc) 1483789Sahrens { 1484789Sahrens spa_t *spa; 1485789Sahrens int error; 1486789Sahrens 14872926Sek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1488789Sahrens return (error); 1489789Sahrens 14908241SJeff.Bonwick@Sun.COM error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE); 1491789Sahrens 1492789Sahrens spa_close(spa, FTAG); 1493789Sahrens return (error); 1494789Sahrens } 1495789Sahrens 1496789Sahrens static int 149711422SMark.Musante@Sun.COM zfs_ioc_vdev_split(zfs_cmd_t *zc) 149811422SMark.Musante@Sun.COM { 149911422SMark.Musante@Sun.COM spa_t *spa; 150011422SMark.Musante@Sun.COM nvlist_t *config, *props = NULL; 150111422SMark.Musante@Sun.COM int error; 150211422SMark.Musante@Sun.COM boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT); 150311422SMark.Musante@Sun.COM 150411422SMark.Musante@Sun.COM if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 150511422SMark.Musante@Sun.COM return (error); 150611422SMark.Musante@Sun.COM 150711422SMark.Musante@Sun.COM if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 150811422SMark.Musante@Sun.COM zc->zc_iflags, &config)) { 150911422SMark.Musante@Sun.COM spa_close(spa, FTAG); 151011422SMark.Musante@Sun.COM return (error); 151111422SMark.Musante@Sun.COM } 151211422SMark.Musante@Sun.COM 151311422SMark.Musante@Sun.COM if (zc->zc_nvlist_src_size != 0 && (error = 151411422SMark.Musante@Sun.COM get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 151511422SMark.Musante@Sun.COM zc->zc_iflags, &props))) { 151611422SMark.Musante@Sun.COM spa_close(spa, FTAG); 151711422SMark.Musante@Sun.COM nvlist_free(config); 151811422SMark.Musante@Sun.COM return (error); 151911422SMark.Musante@Sun.COM } 152011422SMark.Musante@Sun.COM 152111422SMark.Musante@Sun.COM error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp); 152211422SMark.Musante@Sun.COM 152311422SMark.Musante@Sun.COM spa_close(spa, FTAG); 152411422SMark.Musante@Sun.COM 152511422SMark.Musante@Sun.COM nvlist_free(config); 152611422SMark.Musante@Sun.COM nvlist_free(props); 152711422SMark.Musante@Sun.COM 152811422SMark.Musante@Sun.COM return (error); 152911422SMark.Musante@Sun.COM } 153011422SMark.Musante@Sun.COM 153111422SMark.Musante@Sun.COM static int 15321354Seschrock zfs_ioc_vdev_setpath(zfs_cmd_t *zc) 15331354Seschrock { 15341354Seschrock spa_t *spa; 15352676Seschrock char *path = zc->zc_value; 15361544Seschrock uint64_t guid = zc->zc_guid; 15371354Seschrock int error; 15381354Seschrock 15391354Seschrock error = spa_open(zc->zc_name, &spa, FTAG); 15401354Seschrock if (error != 0) 15411354Seschrock return (error); 15421354Seschrock 15431354Seschrock error = spa_vdev_setpath(spa, guid, path); 15441354Seschrock spa_close(spa, FTAG); 15451354Seschrock return (error); 15461354Seschrock } 15471354Seschrock 15489425SEric.Schrock@Sun.COM static int 15499425SEric.Schrock@Sun.COM zfs_ioc_vdev_setfru(zfs_cmd_t *zc) 15509425SEric.Schrock@Sun.COM { 15519425SEric.Schrock@Sun.COM spa_t *spa; 15529425SEric.Schrock@Sun.COM char *fru = zc->zc_value; 15539425SEric.Schrock@Sun.COM uint64_t guid = zc->zc_guid; 15549425SEric.Schrock@Sun.COM int error; 15559425SEric.Schrock@Sun.COM 15569425SEric.Schrock@Sun.COM error = spa_open(zc->zc_name, &spa, FTAG); 15579425SEric.Schrock@Sun.COM if (error != 0) 15589425SEric.Schrock@Sun.COM return (error); 15599425SEric.Schrock@Sun.COM 15609425SEric.Schrock@Sun.COM error = spa_vdev_setfru(spa, guid, fru); 15619425SEric.Schrock@Sun.COM spa_close(spa, FTAG); 15629425SEric.Schrock@Sun.COM return (error); 15639425SEric.Schrock@Sun.COM } 15649425SEric.Schrock@Sun.COM 15655367Sahrens /* 15665367Sahrens * inputs: 15675367Sahrens * zc_name name of filesystem 15685367Sahrens * zc_nvlist_dst_size size of buffer for property nvlist 15695367Sahrens * 15705367Sahrens * outputs: 15715367Sahrens * zc_objset_stats stats 15725367Sahrens * zc_nvlist_dst property nvlist 15735367Sahrens * zc_nvlist_dst_size size of property nvlist 15745367Sahrens */ 15751354Seschrock static int 1576789Sahrens zfs_ioc_objset_stats(zfs_cmd_t *zc) 1577789Sahrens { 1578789Sahrens objset_t *os = NULL; 1579789Sahrens int error; 15801356Seschrock nvlist_t *nv; 1581789Sahrens 158210298SMatthew.Ahrens@Sun.COM if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) 1583789Sahrens return (error); 1584789Sahrens 15852885Sahrens dmu_objset_fast_stat(os, &zc->zc_objset_stats); 1586789Sahrens 15872856Snd150628 if (zc->zc_nvlist_dst != 0 && 158811022STom.Erickson@Sun.COM (error = dsl_prop_get_all(os, &nv)) == 0) { 15892885Sahrens dmu_objset_stats(os, nv); 15903087Sahrens /* 15915147Srm160521 * NB: zvol_get_stats() will read the objset contents, 15923087Sahrens * which we aren't supposed to do with a 15936689Smaybee * DS_MODE_USER hold, because it could be 15943087Sahrens * inconsistent. So this is a bit of a workaround... 159510298SMatthew.Ahrens@Sun.COM * XXX reading with out owning 15963087Sahrens */ 15974577Sahrens if (!zc->zc_objset_stats.dds_inconsistent) { 15984577Sahrens if (dmu_objset_type(os) == DMU_OST_ZVOL) 15994577Sahrens VERIFY(zvol_get_stats(os, nv) == 0); 16004577Sahrens } 16012676Seschrock error = put_nvlist(zc, nv); 16021356Seschrock nvlist_free(nv); 16031356Seschrock } 1604789Sahrens 160510298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 1606789Sahrens return (error); 1607789Sahrens } 1608789Sahrens 160911022STom.Erickson@Sun.COM /* 161011022STom.Erickson@Sun.COM * inputs: 161111022STom.Erickson@Sun.COM * zc_name name of filesystem 161211022STom.Erickson@Sun.COM * zc_nvlist_dst_size size of buffer for property nvlist 161311022STom.Erickson@Sun.COM * 161411022STom.Erickson@Sun.COM * outputs: 161511022STom.Erickson@Sun.COM * zc_nvlist_dst received property nvlist 161611022STom.Erickson@Sun.COM * zc_nvlist_dst_size size of received property nvlist 161711022STom.Erickson@Sun.COM * 161811022STom.Erickson@Sun.COM * Gets received properties (distinct from local properties on or after 161911022STom.Erickson@Sun.COM * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from 162011022STom.Erickson@Sun.COM * local property values. 162111022STom.Erickson@Sun.COM */ 162211022STom.Erickson@Sun.COM static int 162311022STom.Erickson@Sun.COM zfs_ioc_objset_recvd_props(zfs_cmd_t *zc) 162411022STom.Erickson@Sun.COM { 162511022STom.Erickson@Sun.COM objset_t *os = NULL; 162611022STom.Erickson@Sun.COM int error; 162711022STom.Erickson@Sun.COM nvlist_t *nv; 162811022STom.Erickson@Sun.COM 162911022STom.Erickson@Sun.COM if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) 163011022STom.Erickson@Sun.COM return (error); 163111022STom.Erickson@Sun.COM 163211022STom.Erickson@Sun.COM /* 163311022STom.Erickson@Sun.COM * Without this check, we would return local property values if the 163411022STom.Erickson@Sun.COM * caller has not already received properties on or after 163511022STom.Erickson@Sun.COM * SPA_VERSION_RECVD_PROPS. 163611022STom.Erickson@Sun.COM */ 163711022STom.Erickson@Sun.COM if (!dsl_prop_get_hasrecvd(os)) { 163811022STom.Erickson@Sun.COM dmu_objset_rele(os, FTAG); 163911022STom.Erickson@Sun.COM return (ENOTSUP); 164011022STom.Erickson@Sun.COM } 164111022STom.Erickson@Sun.COM 164211022STom.Erickson@Sun.COM if (zc->zc_nvlist_dst != 0 && 164311022STom.Erickson@Sun.COM (error = dsl_prop_get_received(os, &nv)) == 0) { 164411022STom.Erickson@Sun.COM error = put_nvlist(zc, nv); 164511022STom.Erickson@Sun.COM nvlist_free(nv); 164611022STom.Erickson@Sun.COM } 164711022STom.Erickson@Sun.COM 164811022STom.Erickson@Sun.COM dmu_objset_rele(os, FTAG); 164911022STom.Erickson@Sun.COM return (error); 165011022STom.Erickson@Sun.COM } 165111022STom.Erickson@Sun.COM 16525498Stimh static int 16535498Stimh nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop) 16545498Stimh { 16555498Stimh uint64_t value; 16565498Stimh int error; 16575498Stimh 16585498Stimh /* 16595498Stimh * zfs_get_zplprop() will either find a value or give us 16605498Stimh * the default value (if there is one). 16615498Stimh */ 16625498Stimh if ((error = zfs_get_zplprop(os, prop, &value)) != 0) 16635498Stimh return (error); 16645498Stimh VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0); 16655498Stimh return (0); 16665498Stimh } 16675498Stimh 16685498Stimh /* 16695498Stimh * inputs: 16705498Stimh * zc_name name of filesystem 16715498Stimh * zc_nvlist_dst_size size of buffer for zpl property nvlist 16725498Stimh * 16735498Stimh * outputs: 16745498Stimh * zc_nvlist_dst zpl property nvlist 16755498Stimh * zc_nvlist_dst_size size of zpl property nvlist 16765498Stimh */ 16775498Stimh static int 16785498Stimh zfs_ioc_objset_zplprops(zfs_cmd_t *zc) 16795498Stimh { 16805498Stimh objset_t *os; 16815498Stimh int err; 16825498Stimh 168310298SMatthew.Ahrens@Sun.COM /* XXX reading without owning */ 168410298SMatthew.Ahrens@Sun.COM if (err = dmu_objset_hold(zc->zc_name, FTAG, &os)) 16855498Stimh return (err); 16865498Stimh 16875498Stimh dmu_objset_fast_stat(os, &zc->zc_objset_stats); 16885498Stimh 16895498Stimh /* 16905498Stimh * NB: nvl_add_zplprop() will read the objset contents, 16916689Smaybee * which we aren't supposed to do with a DS_MODE_USER 16926689Smaybee * hold, because it could be inconsistent. 16935498Stimh */ 16945498Stimh if (zc->zc_nvlist_dst != NULL && 16955498Stimh !zc->zc_objset_stats.dds_inconsistent && 16965498Stimh dmu_objset_type(os) == DMU_OST_ZFS) { 16975498Stimh nvlist_t *nv; 16985498Stimh 16995498Stimh VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 17005498Stimh if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 && 17015498Stimh (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 && 17025498Stimh (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 && 17035498Stimh (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0) 17045498Stimh err = put_nvlist(zc, nv); 17055498Stimh nvlist_free(nv); 17065498Stimh } else { 17075498Stimh err = ENOENT; 17085498Stimh } 170910298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 17105498Stimh return (err); 17115498Stimh } 17125498Stimh 17139396SMatthew.Ahrens@Sun.COM static boolean_t 17149396SMatthew.Ahrens@Sun.COM dataset_name_hidden(const char *name) 17159396SMatthew.Ahrens@Sun.COM { 17169396SMatthew.Ahrens@Sun.COM /* 17179396SMatthew.Ahrens@Sun.COM * Skip over datasets that are not visible in this zone, 17189396SMatthew.Ahrens@Sun.COM * internal datasets (which have a $ in their name), and 17199396SMatthew.Ahrens@Sun.COM * temporary datasets (which have a % in their name). 17209396SMatthew.Ahrens@Sun.COM */ 17219396SMatthew.Ahrens@Sun.COM if (strchr(name, '$') != NULL) 17229396SMatthew.Ahrens@Sun.COM return (B_TRUE); 17239396SMatthew.Ahrens@Sun.COM if (strchr(name, '%') != NULL) 17249396SMatthew.Ahrens@Sun.COM return (B_TRUE); 17259396SMatthew.Ahrens@Sun.COM if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL)) 17269396SMatthew.Ahrens@Sun.COM return (B_TRUE); 17279396SMatthew.Ahrens@Sun.COM return (B_FALSE); 17289396SMatthew.Ahrens@Sun.COM } 17299396SMatthew.Ahrens@Sun.COM 17305367Sahrens /* 17315367Sahrens * inputs: 17325367Sahrens * zc_name name of filesystem 17335367Sahrens * zc_cookie zap cursor 17345367Sahrens * zc_nvlist_dst_size size of buffer for property nvlist 17355367Sahrens * 17365367Sahrens * outputs: 17375367Sahrens * zc_name name of next filesystem 17389396SMatthew.Ahrens@Sun.COM * zc_cookie zap cursor 17395367Sahrens * zc_objset_stats stats 17405367Sahrens * zc_nvlist_dst property nvlist 17415367Sahrens * zc_nvlist_dst_size size of property nvlist 17425367Sahrens */ 1743789Sahrens static int 1744789Sahrens zfs_ioc_dataset_list_next(zfs_cmd_t *zc) 1745789Sahrens { 1746885Sahrens objset_t *os; 1747789Sahrens int error; 1748789Sahrens char *p; 174911546SChris.Kirby@sun.com size_t orig_len = strlen(zc->zc_name); 175011546SChris.Kirby@sun.com 175111546SChris.Kirby@sun.com top: 175210298SMatthew.Ahrens@Sun.COM if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) { 1753885Sahrens if (error == ENOENT) 1754885Sahrens error = ESRCH; 1755885Sahrens return (error); 1756789Sahrens } 1757789Sahrens 1758789Sahrens p = strrchr(zc->zc_name, '/'); 1759789Sahrens if (p == NULL || p[1] != '\0') 1760789Sahrens (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name)); 1761789Sahrens p = zc->zc_name + strlen(zc->zc_name); 1762789Sahrens 17638697SRichard.Morris@Sun.COM /* 17648697SRichard.Morris@Sun.COM * Pre-fetch the datasets. dmu_objset_prefetch() always returns 0 17658697SRichard.Morris@Sun.COM * but is not declared void because its called by dmu_objset_find(). 17668697SRichard.Morris@Sun.COM */ 17678415SRichard.Morris@Sun.COM if (zc->zc_cookie == 0) { 17688415SRichard.Morris@Sun.COM uint64_t cookie = 0; 17698415SRichard.Morris@Sun.COM int len = sizeof (zc->zc_name) - (p - zc->zc_name); 17708415SRichard.Morris@Sun.COM 17718415SRichard.Morris@Sun.COM while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0) 17728697SRichard.Morris@Sun.COM (void) dmu_objset_prefetch(p, NULL); 17738415SRichard.Morris@Sun.COM } 17748415SRichard.Morris@Sun.COM 1775789Sahrens do { 1776885Sahrens error = dmu_dir_list_next(os, 1777885Sahrens sizeof (zc->zc_name) - (p - zc->zc_name), p, 1778885Sahrens NULL, &zc->zc_cookie); 1779789Sahrens if (error == ENOENT) 1780789Sahrens error = ESRCH; 178110588SEric.Taylor@Sun.COM } while (error == 0 && dataset_name_hidden(zc->zc_name) && 178210588SEric.Taylor@Sun.COM !(zc->zc_iflags & FKIOCTL)); 178310298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 1784789Sahrens 178510588SEric.Taylor@Sun.COM /* 178610588SEric.Taylor@Sun.COM * If it's an internal dataset (ie. with a '$' in its name), 178710588SEric.Taylor@Sun.COM * don't try to get stats for it, otherwise we'll return ENOENT. 178810588SEric.Taylor@Sun.COM */ 178911546SChris.Kirby@sun.com if (error == 0 && strchr(zc->zc_name, '$') == NULL) { 1790885Sahrens error = zfs_ioc_objset_stats(zc); /* fill in the stats */ 179111546SChris.Kirby@sun.com if (error == ENOENT) { 179211546SChris.Kirby@sun.com /* We lost a race with destroy, get the next one. */ 179311546SChris.Kirby@sun.com zc->zc_name[orig_len] = '\0'; 179411546SChris.Kirby@sun.com goto top; 179511546SChris.Kirby@sun.com } 179611546SChris.Kirby@sun.com } 1797789Sahrens return (error); 1798789Sahrens } 1799789Sahrens 18005367Sahrens /* 18015367Sahrens * inputs: 18025367Sahrens * zc_name name of filesystem 18035367Sahrens * zc_cookie zap cursor 18045367Sahrens * zc_nvlist_dst_size size of buffer for property nvlist 18055367Sahrens * 18065367Sahrens * outputs: 18075367Sahrens * zc_name name of next snapshot 18085367Sahrens * zc_objset_stats stats 18095367Sahrens * zc_nvlist_dst property nvlist 18105367Sahrens * zc_nvlist_dst_size size of property nvlist 18115367Sahrens */ 1812789Sahrens static int 1813789Sahrens zfs_ioc_snapshot_list_next(zfs_cmd_t *zc) 1814789Sahrens { 1815885Sahrens objset_t *os; 1816789Sahrens int error; 1817789Sahrens 181811546SChris.Kirby@sun.com top: 181910474SRichard.Morris@Sun.COM if (zc->zc_cookie == 0) 182010474SRichard.Morris@Sun.COM (void) dmu_objset_find(zc->zc_name, dmu_objset_prefetch, 182110474SRichard.Morris@Sun.COM NULL, DS_FIND_SNAPSHOTS); 182210474SRichard.Morris@Sun.COM 182310298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(zc->zc_name, FTAG, &os); 18246689Smaybee if (error) 18256689Smaybee return (error == ENOENT ? ESRCH : error); 1826789Sahrens 18271003Slling /* 18281003Slling * A dataset name of maximum length cannot have any snapshots, 18291003Slling * so exit immediately. 18301003Slling */ 18311003Slling if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) { 183210298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 18331003Slling return (ESRCH); 1834789Sahrens } 1835789Sahrens 1836885Sahrens error = dmu_snapshot_list_next(os, 1837885Sahrens sizeof (zc->zc_name) - strlen(zc->zc_name), 18385663Sck153898 zc->zc_name + strlen(zc->zc_name), NULL, &zc->zc_cookie, NULL); 183910298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 184011546SChris.Kirby@sun.com if (error == 0) { 1841885Sahrens error = zfs_ioc_objset_stats(zc); /* fill in the stats */ 184211546SChris.Kirby@sun.com if (error == ENOENT) { 184311546SChris.Kirby@sun.com /* We lost a race with destroy, get the next one. */ 184411546SChris.Kirby@sun.com *strchr(zc->zc_name, '@') = '\0'; 184511546SChris.Kirby@sun.com goto top; 184611546SChris.Kirby@sun.com } 184711546SChris.Kirby@sun.com } else if (error == ENOENT) { 18486689Smaybee error = ESRCH; 184911546SChris.Kirby@sun.com } 1850789Sahrens 18515367Sahrens /* if we failed, undo the @ that we tacked on to zc_name */ 18526689Smaybee if (error) 18535367Sahrens *strchr(zc->zc_name, '@') = '\0'; 1854789Sahrens return (error); 1855789Sahrens } 1856789Sahrens 185711022STom.Erickson@Sun.COM static int 185811022STom.Erickson@Sun.COM zfs_prop_set_userquota(const char *dsname, nvpair_t *pair) 185911022STom.Erickson@Sun.COM { 186011022STom.Erickson@Sun.COM const char *propname = nvpair_name(pair); 186111022STom.Erickson@Sun.COM uint64_t *valary; 186211022STom.Erickson@Sun.COM unsigned int vallen; 186311022STom.Erickson@Sun.COM const char *domain; 1864*11933STim.Haley@Sun.COM char *dash; 186511022STom.Erickson@Sun.COM zfs_userquota_prop_t type; 186611022STom.Erickson@Sun.COM uint64_t rid; 186711022STom.Erickson@Sun.COM uint64_t quota; 186811022STom.Erickson@Sun.COM zfsvfs_t *zfsvfs; 186911022STom.Erickson@Sun.COM int err; 187011022STom.Erickson@Sun.COM 187111022STom.Erickson@Sun.COM if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 187211022STom.Erickson@Sun.COM nvlist_t *attrs; 187311022STom.Erickson@Sun.COM VERIFY(nvpair_value_nvlist(pair, &attrs) == 0); 1874*11933STim.Haley@Sun.COM if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 1875*11933STim.Haley@Sun.COM &pair) != 0) 1876*11933STim.Haley@Sun.COM return (EINVAL); 187711022STom.Erickson@Sun.COM } 187811022STom.Erickson@Sun.COM 1879*11933STim.Haley@Sun.COM /* 1880*11933STim.Haley@Sun.COM * A correctly constructed propname is encoded as 1881*11933STim.Haley@Sun.COM * userquota@<rid>-<domain>. 1882*11933STim.Haley@Sun.COM */ 1883*11933STim.Haley@Sun.COM if ((dash = strchr(propname, '-')) == NULL || 1884*11933STim.Haley@Sun.COM nvpair_value_uint64_array(pair, &valary, &vallen) != 0 || 1885*11933STim.Haley@Sun.COM vallen != 3) 1886*11933STim.Haley@Sun.COM return (EINVAL); 1887*11933STim.Haley@Sun.COM 1888*11933STim.Haley@Sun.COM domain = dash + 1; 188911022STom.Erickson@Sun.COM type = valary[0]; 189011022STom.Erickson@Sun.COM rid = valary[1]; 189111022STom.Erickson@Sun.COM quota = valary[2]; 189211022STom.Erickson@Sun.COM 189311022STom.Erickson@Sun.COM err = zfsvfs_hold(dsname, FTAG, &zfsvfs); 189411022STom.Erickson@Sun.COM if (err == 0) { 189511022STom.Erickson@Sun.COM err = zfs_set_userquota(zfsvfs, type, domain, rid, quota); 189611022STom.Erickson@Sun.COM zfsvfs_rele(zfsvfs, FTAG); 189711022STom.Erickson@Sun.COM } 189811022STom.Erickson@Sun.COM 189911022STom.Erickson@Sun.COM return (err); 190011022STom.Erickson@Sun.COM } 190111022STom.Erickson@Sun.COM 190211022STom.Erickson@Sun.COM /* 190311022STom.Erickson@Sun.COM * If the named property is one that has a special function to set its value, 190411022STom.Erickson@Sun.COM * return 0 on success and a positive error code on failure; otherwise if it is 190511022STom.Erickson@Sun.COM * not one of the special properties handled by this function, return -1. 190611022STom.Erickson@Sun.COM * 1907*11933STim.Haley@Sun.COM * XXX: It would be better for callers of the property interface if we handled 190811022STom.Erickson@Sun.COM * these special cases in dsl_prop.c (in the dsl layer). 190911022STom.Erickson@Sun.COM */ 191011022STom.Erickson@Sun.COM static int 191111022STom.Erickson@Sun.COM zfs_prop_set_special(const char *dsname, zprop_source_t source, 191211022STom.Erickson@Sun.COM nvpair_t *pair) 1913789Sahrens { 191411022STom.Erickson@Sun.COM const char *propname = nvpair_name(pair); 191511022STom.Erickson@Sun.COM zfs_prop_t prop = zfs_name_to_prop(propname); 191611022STom.Erickson@Sun.COM uint64_t intval; 191711022STom.Erickson@Sun.COM int err; 191811022STom.Erickson@Sun.COM 191911022STom.Erickson@Sun.COM if (prop == ZPROP_INVAL) { 192011022STom.Erickson@Sun.COM if (zfs_prop_userquota(propname)) 192111022STom.Erickson@Sun.COM return (zfs_prop_set_userquota(dsname, pair)); 192211022STom.Erickson@Sun.COM return (-1); 192311022STom.Erickson@Sun.COM } 192411022STom.Erickson@Sun.COM 192511022STom.Erickson@Sun.COM if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 192611022STom.Erickson@Sun.COM nvlist_t *attrs; 192711022STom.Erickson@Sun.COM VERIFY(nvpair_value_nvlist(pair, &attrs) == 0); 192811022STom.Erickson@Sun.COM VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 192911022STom.Erickson@Sun.COM &pair) == 0); 193011022STom.Erickson@Sun.COM } 193111022STom.Erickson@Sun.COM 193211022STom.Erickson@Sun.COM if (zfs_prop_get_type(prop) == PROP_TYPE_STRING) 193311022STom.Erickson@Sun.COM return (-1); 193411022STom.Erickson@Sun.COM 193511022STom.Erickson@Sun.COM VERIFY(0 == nvpair_value_uint64(pair, &intval)); 193611022STom.Erickson@Sun.COM 193711022STom.Erickson@Sun.COM switch (prop) { 193811022STom.Erickson@Sun.COM case ZFS_PROP_QUOTA: 193911022STom.Erickson@Sun.COM err = dsl_dir_set_quota(dsname, source, intval); 194011022STom.Erickson@Sun.COM break; 194111022STom.Erickson@Sun.COM case ZFS_PROP_REFQUOTA: 194211022STom.Erickson@Sun.COM err = dsl_dataset_set_quota(dsname, source, intval); 194311022STom.Erickson@Sun.COM break; 194411022STom.Erickson@Sun.COM case ZFS_PROP_RESERVATION: 194511022STom.Erickson@Sun.COM err = dsl_dir_set_reservation(dsname, source, intval); 194611022STom.Erickson@Sun.COM break; 194711022STom.Erickson@Sun.COM case ZFS_PROP_REFRESERVATION: 194811022STom.Erickson@Sun.COM err = dsl_dataset_set_reservation(dsname, source, intval); 194911022STom.Erickson@Sun.COM break; 195011022STom.Erickson@Sun.COM case ZFS_PROP_VOLSIZE: 195111022STom.Erickson@Sun.COM err = zvol_set_volsize(dsname, ddi_driver_major(zfs_dip), 195211022STom.Erickson@Sun.COM intval); 195311022STom.Erickson@Sun.COM break; 195411022STom.Erickson@Sun.COM case ZFS_PROP_VERSION: 195511022STom.Erickson@Sun.COM { 195611022STom.Erickson@Sun.COM zfsvfs_t *zfsvfs; 195711889SRichard.Morris@Sun.COM uint64_t maxzplver = ZPL_VERSION; 195811022STom.Erickson@Sun.COM 195911022STom.Erickson@Sun.COM if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs)) != 0) 196011022STom.Erickson@Sun.COM break; 196111022STom.Erickson@Sun.COM 196211889SRichard.Morris@Sun.COM if (zfs_earlier_version(dsname, SPA_VERSION_USERSPACE)) 196311889SRichard.Morris@Sun.COM maxzplver = ZPL_VERSION_USERSPACE - 1; 196411889SRichard.Morris@Sun.COM if (zfs_earlier_version(dsname, SPA_VERSION_FUID)) 196511889SRichard.Morris@Sun.COM maxzplver = ZPL_VERSION_FUID - 1; 196611889SRichard.Morris@Sun.COM if (intval > maxzplver) { 196711889SRichard.Morris@Sun.COM zfsvfs_rele(zfsvfs, FTAG); 196811889SRichard.Morris@Sun.COM return (ENOTSUP); 196911889SRichard.Morris@Sun.COM } 197011889SRichard.Morris@Sun.COM 197111022STom.Erickson@Sun.COM err = zfs_set_version(zfsvfs, intval); 197211022STom.Erickson@Sun.COM zfsvfs_rele(zfsvfs, FTAG); 197311022STom.Erickson@Sun.COM 197411022STom.Erickson@Sun.COM if (err == 0 && intval >= ZPL_VERSION_USERSPACE) { 197511147SGeorge.Wilson@Sun.COM zfs_cmd_t *zc; 197611147SGeorge.Wilson@Sun.COM 197711147SGeorge.Wilson@Sun.COM zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP); 197811147SGeorge.Wilson@Sun.COM (void) strcpy(zc->zc_name, dsname); 197911147SGeorge.Wilson@Sun.COM (void) zfs_ioc_userspace_upgrade(zc); 198011147SGeorge.Wilson@Sun.COM kmem_free(zc, sizeof (zfs_cmd_t)); 198111022STom.Erickson@Sun.COM } 198211022STom.Erickson@Sun.COM break; 198311022STom.Erickson@Sun.COM } 198411022STom.Erickson@Sun.COM 198511022STom.Erickson@Sun.COM default: 198611022STom.Erickson@Sun.COM err = -1; 198711022STom.Erickson@Sun.COM } 198811022STom.Erickson@Sun.COM 198911022STom.Erickson@Sun.COM return (err); 199011022STom.Erickson@Sun.COM } 199111022STom.Erickson@Sun.COM 199211022STom.Erickson@Sun.COM /* 199311022STom.Erickson@Sun.COM * This function is best effort. If it fails to set any of the given properties, 199411022STom.Erickson@Sun.COM * it continues to set as many as it can and returns the first error 199511022STom.Erickson@Sun.COM * encountered. If the caller provides a non-NULL errlist, it also gives the 199611022STom.Erickson@Sun.COM * complete list of names of all the properties it failed to set along with the 199711022STom.Erickson@Sun.COM * corresponding error numbers. The caller is responsible for freeing the 199811022STom.Erickson@Sun.COM * returned errlist. 199911022STom.Erickson@Sun.COM * 200011022STom.Erickson@Sun.COM * If every property is set successfully, zero is returned and the list pointed 200111022STom.Erickson@Sun.COM * at by errlist is NULL. 200211022STom.Erickson@Sun.COM */ 200311022STom.Erickson@Sun.COM int 200411022STom.Erickson@Sun.COM zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl, 200511022STom.Erickson@Sun.COM nvlist_t **errlist) 200611022STom.Erickson@Sun.COM { 200711022STom.Erickson@Sun.COM nvpair_t *pair; 200811022STom.Erickson@Sun.COM nvpair_t *propval; 200911045STom.Erickson@Sun.COM int rv = 0; 20102676Seschrock uint64_t intval; 20112676Seschrock char *strval; 20128697SRichard.Morris@Sun.COM nvlist_t *genericnvl; 201311022STom.Erickson@Sun.COM nvlist_t *errors; 201411022STom.Erickson@Sun.COM nvlist_t *retrynvl; 20154543Smarks 20168697SRichard.Morris@Sun.COM VERIFY(nvlist_alloc(&genericnvl, NV_UNIQUE_NAME, KM_SLEEP) == 0); 201711022STom.Erickson@Sun.COM VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0); 201811022STom.Erickson@Sun.COM VERIFY(nvlist_alloc(&retrynvl, NV_UNIQUE_NAME, KM_SLEEP) == 0); 201911022STom.Erickson@Sun.COM 202011022STom.Erickson@Sun.COM retry: 202111022STom.Erickson@Sun.COM pair = NULL; 202211022STom.Erickson@Sun.COM while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) { 202311022STom.Erickson@Sun.COM const char *propname = nvpair_name(pair); 20244670Sahrens zfs_prop_t prop = zfs_name_to_prop(propname); 202511181STom.Erickson@Sun.COM int err = 0; 20264543Smarks 202711022STom.Erickson@Sun.COM /* decode the property value */ 202811022STom.Erickson@Sun.COM propval = pair; 202911022STom.Erickson@Sun.COM if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 203011022STom.Erickson@Sun.COM nvlist_t *attrs; 203111022STom.Erickson@Sun.COM VERIFY(nvpair_value_nvlist(pair, &attrs) == 0); 2032*11933STim.Haley@Sun.COM if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 2033*11933STim.Haley@Sun.COM &propval) != 0) 2034*11933STim.Haley@Sun.COM err = EINVAL; 20354543Smarks } 20362676Seschrock 203711022STom.Erickson@Sun.COM /* Validate value type */ 2038*11933STim.Haley@Sun.COM if (err == 0 && prop == ZPROP_INVAL) { 203911022STom.Erickson@Sun.COM if (zfs_prop_user(propname)) { 204011022STom.Erickson@Sun.COM if (nvpair_type(propval) != DATA_TYPE_STRING) 204111022STom.Erickson@Sun.COM err = EINVAL; 204211022STom.Erickson@Sun.COM } else if (zfs_prop_userquota(propname)) { 204311022STom.Erickson@Sun.COM if (nvpair_type(propval) != 204411022STom.Erickson@Sun.COM DATA_TYPE_UINT64_ARRAY) 204511022STom.Erickson@Sun.COM err = EINVAL; 20469396SMatthew.Ahrens@Sun.COM } 2047*11933STim.Haley@Sun.COM } else if (err == 0) { 204811022STom.Erickson@Sun.COM if (nvpair_type(propval) == DATA_TYPE_STRING) { 204911022STom.Erickson@Sun.COM if (zfs_prop_get_type(prop) != PROP_TYPE_STRING) 205011022STom.Erickson@Sun.COM err = EINVAL; 205111022STom.Erickson@Sun.COM } else if (nvpair_type(propval) == DATA_TYPE_UINT64) { 20522885Sahrens const char *unused; 20532885Sahrens 205411022STom.Erickson@Sun.COM VERIFY(nvpair_value_uint64(propval, 205511022STom.Erickson@Sun.COM &intval) == 0); 20562676Seschrock 20572676Seschrock switch (zfs_prop_get_type(prop)) { 20584787Sahrens case PROP_TYPE_NUMBER: 20592676Seschrock break; 20604787Sahrens case PROP_TYPE_STRING: 206111022STom.Erickson@Sun.COM err = EINVAL; 206211022STom.Erickson@Sun.COM break; 20634787Sahrens case PROP_TYPE_INDEX: 20642717Seschrock if (zfs_prop_index_to_string(prop, 206511022STom.Erickson@Sun.COM intval, &unused) != 0) 206611022STom.Erickson@Sun.COM err = EINVAL; 20672676Seschrock break; 20682676Seschrock default: 20694577Sahrens cmn_err(CE_PANIC, 20704577Sahrens "unknown property type"); 20712676Seschrock } 20722676Seschrock } else { 207311022STom.Erickson@Sun.COM err = EINVAL; 207411022STom.Erickson@Sun.COM } 207511022STom.Erickson@Sun.COM } 207611022STom.Erickson@Sun.COM 207711022STom.Erickson@Sun.COM /* Validate permissions */ 207811022STom.Erickson@Sun.COM if (err == 0) 207911022STom.Erickson@Sun.COM err = zfs_check_settable(dsname, pair, CRED()); 208011022STom.Erickson@Sun.COM 208111022STom.Erickson@Sun.COM if (err == 0) { 208211022STom.Erickson@Sun.COM err = zfs_prop_set_special(dsname, source, pair); 208311022STom.Erickson@Sun.COM if (err == -1) { 208411022STom.Erickson@Sun.COM /* 208511022STom.Erickson@Sun.COM * For better performance we build up a list of 208611022STom.Erickson@Sun.COM * properties to set in a single transaction. 208711022STom.Erickson@Sun.COM */ 208811022STom.Erickson@Sun.COM err = nvlist_add_nvpair(genericnvl, pair); 208911022STom.Erickson@Sun.COM } else if (err != 0 && nvl != retrynvl) { 209011022STom.Erickson@Sun.COM /* 209111022STom.Erickson@Sun.COM * This may be a spurious error caused by 209211022STom.Erickson@Sun.COM * receiving quota and reservation out of order. 209311022STom.Erickson@Sun.COM * Try again in a second pass. 209411022STom.Erickson@Sun.COM */ 209511022STom.Erickson@Sun.COM err = nvlist_add_nvpair(retrynvl, pair); 20962676Seschrock } 209711022STom.Erickson@Sun.COM } 209811022STom.Erickson@Sun.COM 209911022STom.Erickson@Sun.COM if (err != 0) 210011022STom.Erickson@Sun.COM VERIFY(nvlist_add_int32(errors, propname, err) == 0); 210111022STom.Erickson@Sun.COM } 210211022STom.Erickson@Sun.COM 210311022STom.Erickson@Sun.COM if (nvl != retrynvl && !nvlist_empty(retrynvl)) { 210411022STom.Erickson@Sun.COM nvl = retrynvl; 210511022STom.Erickson@Sun.COM goto retry; 210611022STom.Erickson@Sun.COM } 210711022STom.Erickson@Sun.COM 210811022STom.Erickson@Sun.COM if (!nvlist_empty(genericnvl) && 210911022STom.Erickson@Sun.COM dsl_props_set(dsname, source, genericnvl) != 0) { 211011022STom.Erickson@Sun.COM /* 211111022STom.Erickson@Sun.COM * If this fails, we still want to set as many properties as we 211211022STom.Erickson@Sun.COM * can, so try setting them individually. 211311022STom.Erickson@Sun.COM */ 211411022STom.Erickson@Sun.COM pair = NULL; 211511022STom.Erickson@Sun.COM while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) { 211611022STom.Erickson@Sun.COM const char *propname = nvpair_name(pair); 211711181STom.Erickson@Sun.COM int err = 0; 211811022STom.Erickson@Sun.COM 211911022STom.Erickson@Sun.COM propval = pair; 212011022STom.Erickson@Sun.COM if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 212111022STom.Erickson@Sun.COM nvlist_t *attrs; 212211022STom.Erickson@Sun.COM VERIFY(nvpair_value_nvlist(pair, &attrs) == 0); 212311022STom.Erickson@Sun.COM VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 212411022STom.Erickson@Sun.COM &propval) == 0); 212511022STom.Erickson@Sun.COM } 212611022STom.Erickson@Sun.COM 212711022STom.Erickson@Sun.COM if (nvpair_type(propval) == DATA_TYPE_STRING) { 212811022STom.Erickson@Sun.COM VERIFY(nvpair_value_string(propval, 212911022STom.Erickson@Sun.COM &strval) == 0); 213011022STom.Erickson@Sun.COM err = dsl_prop_set(dsname, propname, source, 1, 213111022STom.Erickson@Sun.COM strlen(strval) + 1, strval); 213211022STom.Erickson@Sun.COM } else { 213311022STom.Erickson@Sun.COM VERIFY(nvpair_value_uint64(propval, 213411022STom.Erickson@Sun.COM &intval) == 0); 213511022STom.Erickson@Sun.COM err = dsl_prop_set(dsname, propname, source, 8, 213611022STom.Erickson@Sun.COM 1, &intval); 213711022STom.Erickson@Sun.COM } 213811022STom.Erickson@Sun.COM 213911022STom.Erickson@Sun.COM if (err != 0) { 214011022STom.Erickson@Sun.COM VERIFY(nvlist_add_int32(errors, propname, 214111022STom.Erickson@Sun.COM err) == 0); 214211022STom.Erickson@Sun.COM } 21432676Seschrock } 21442676Seschrock } 214511022STom.Erickson@Sun.COM nvlist_free(genericnvl); 214611022STom.Erickson@Sun.COM nvlist_free(retrynvl); 214711022STom.Erickson@Sun.COM 214811022STom.Erickson@Sun.COM if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) { 214911022STom.Erickson@Sun.COM nvlist_free(errors); 215011022STom.Erickson@Sun.COM errors = NULL; 215111022STom.Erickson@Sun.COM } else { 215211022STom.Erickson@Sun.COM VERIFY(nvpair_value_int32(pair, &rv) == 0); 21538697SRichard.Morris@Sun.COM } 215411022STom.Erickson@Sun.COM 215511022STom.Erickson@Sun.COM if (errlist == NULL) 215611022STom.Erickson@Sun.COM nvlist_free(errors); 215711022STom.Erickson@Sun.COM else 215811022STom.Erickson@Sun.COM *errlist = errors; 215911022STom.Erickson@Sun.COM 216011022STom.Erickson@Sun.COM return (rv); 2161789Sahrens } 2162789Sahrens 21635367Sahrens /* 21649355SMatthew.Ahrens@Sun.COM * Check that all the properties are valid user properties. 21659355SMatthew.Ahrens@Sun.COM */ 21669355SMatthew.Ahrens@Sun.COM static int 21679355SMatthew.Ahrens@Sun.COM zfs_check_userprops(char *fsname, nvlist_t *nvl) 21689355SMatthew.Ahrens@Sun.COM { 216911022STom.Erickson@Sun.COM nvpair_t *pair = NULL; 21709355SMatthew.Ahrens@Sun.COM int error = 0; 21719355SMatthew.Ahrens@Sun.COM 217211022STom.Erickson@Sun.COM while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) { 217311022STom.Erickson@Sun.COM const char *propname = nvpair_name(pair); 21749355SMatthew.Ahrens@Sun.COM char *valstr; 21759355SMatthew.Ahrens@Sun.COM 21769355SMatthew.Ahrens@Sun.COM if (!zfs_prop_user(propname) || 217711022STom.Erickson@Sun.COM nvpair_type(pair) != DATA_TYPE_STRING) 21789355SMatthew.Ahrens@Sun.COM return (EINVAL); 21799355SMatthew.Ahrens@Sun.COM 21809355SMatthew.Ahrens@Sun.COM if (error = zfs_secpolicy_write_perms(fsname, 21819355SMatthew.Ahrens@Sun.COM ZFS_DELEG_PERM_USERPROP, CRED())) 21829355SMatthew.Ahrens@Sun.COM return (error); 21839355SMatthew.Ahrens@Sun.COM 21849355SMatthew.Ahrens@Sun.COM if (strlen(propname) >= ZAP_MAXNAMELEN) 21859355SMatthew.Ahrens@Sun.COM return (ENAMETOOLONG); 21869355SMatthew.Ahrens@Sun.COM 218711022STom.Erickson@Sun.COM VERIFY(nvpair_value_string(pair, &valstr) == 0); 21889355SMatthew.Ahrens@Sun.COM if (strlen(valstr) >= ZAP_MAXVALUELEN) 21899355SMatthew.Ahrens@Sun.COM return (E2BIG); 21909355SMatthew.Ahrens@Sun.COM } 21919355SMatthew.Ahrens@Sun.COM return (0); 21929355SMatthew.Ahrens@Sun.COM } 21939355SMatthew.Ahrens@Sun.COM 219411022STom.Erickson@Sun.COM static void 219511022STom.Erickson@Sun.COM props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops) 219611022STom.Erickson@Sun.COM { 219711022STom.Erickson@Sun.COM nvpair_t *pair; 219811022STom.Erickson@Sun.COM 219911022STom.Erickson@Sun.COM VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0); 220011022STom.Erickson@Sun.COM 220111022STom.Erickson@Sun.COM pair = NULL; 220211022STom.Erickson@Sun.COM while ((pair = nvlist_next_nvpair(props, pair)) != NULL) { 220311022STom.Erickson@Sun.COM if (nvlist_exists(skipped, nvpair_name(pair))) 220411022STom.Erickson@Sun.COM continue; 220511022STom.Erickson@Sun.COM 220611022STom.Erickson@Sun.COM VERIFY(nvlist_add_nvpair(*newprops, pair) == 0); 220711022STom.Erickson@Sun.COM } 220811022STom.Erickson@Sun.COM } 220911022STom.Erickson@Sun.COM 221011022STom.Erickson@Sun.COM static int 221111022STom.Erickson@Sun.COM clear_received_props(objset_t *os, const char *fs, nvlist_t *props, 221211022STom.Erickson@Sun.COM nvlist_t *skipped) 221311022STom.Erickson@Sun.COM { 221411022STom.Erickson@Sun.COM int err = 0; 221511022STom.Erickson@Sun.COM nvlist_t *cleared_props = NULL; 221611022STom.Erickson@Sun.COM props_skip(props, skipped, &cleared_props); 221711022STom.Erickson@Sun.COM if (!nvlist_empty(cleared_props)) { 221811022STom.Erickson@Sun.COM /* 221911022STom.Erickson@Sun.COM * Acts on local properties until the dataset has received 222011022STom.Erickson@Sun.COM * properties at least once on or after SPA_VERSION_RECVD_PROPS. 222111022STom.Erickson@Sun.COM */ 222211022STom.Erickson@Sun.COM zprop_source_t flags = (ZPROP_SRC_NONE | 222311022STom.Erickson@Sun.COM (dsl_prop_get_hasrecvd(os) ? ZPROP_SRC_RECEIVED : 0)); 222411022STom.Erickson@Sun.COM err = zfs_set_prop_nvlist(fs, flags, cleared_props, NULL); 222511022STom.Erickson@Sun.COM } 222611022STom.Erickson@Sun.COM nvlist_free(cleared_props); 222711022STom.Erickson@Sun.COM return (err); 222811022STom.Erickson@Sun.COM } 222911022STom.Erickson@Sun.COM 22309355SMatthew.Ahrens@Sun.COM /* 22315367Sahrens * inputs: 22325367Sahrens * zc_name name of filesystem 22338697SRichard.Morris@Sun.COM * zc_value name of property to set 22345367Sahrens * zc_nvlist_src{_size} nvlist of properties to apply 223511022STom.Erickson@Sun.COM * zc_cookie received properties flag 22365367Sahrens * 223711022STom.Erickson@Sun.COM * outputs: 223811022STom.Erickson@Sun.COM * zc_nvlist_dst{_size} error for each unapplied received property 22395367Sahrens */ 2240789Sahrens static int 22412676Seschrock zfs_ioc_set_prop(zfs_cmd_t *zc) 2242789Sahrens { 22432676Seschrock nvlist_t *nvl; 224411022STom.Erickson@Sun.COM boolean_t received = zc->zc_cookie; 224511022STom.Erickson@Sun.COM zprop_source_t source = (received ? ZPROP_SRC_RECEIVED : 224611022STom.Erickson@Sun.COM ZPROP_SRC_LOCAL); 224711022STom.Erickson@Sun.COM nvlist_t *errors = NULL; 22482676Seschrock int error; 2249789Sahrens 22505094Slling if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 22519643SEric.Taylor@Sun.COM zc->zc_iflags, &nvl)) != 0) 22522676Seschrock return (error); 22532676Seschrock 225411022STom.Erickson@Sun.COM if (received) { 22557265Sahrens nvlist_t *origprops; 22567265Sahrens objset_t *os; 22577265Sahrens 225810298SMatthew.Ahrens@Sun.COM if (dmu_objset_hold(zc->zc_name, FTAG, &os) == 0) { 225911022STom.Erickson@Sun.COM if (dsl_prop_get_received(os, &origprops) == 0) { 226011022STom.Erickson@Sun.COM (void) clear_received_props(os, 226111022STom.Erickson@Sun.COM zc->zc_name, origprops, nvl); 22627265Sahrens nvlist_free(origprops); 22637265Sahrens } 226411022STom.Erickson@Sun.COM 226511022STom.Erickson@Sun.COM dsl_prop_set_hasrecvd(os); 226610298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 22677265Sahrens } 22687265Sahrens } 22697265Sahrens 227011022STom.Erickson@Sun.COM error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, &errors); 227111022STom.Erickson@Sun.COM 227211022STom.Erickson@Sun.COM if (zc->zc_nvlist_dst != NULL && errors != NULL) { 227311022STom.Erickson@Sun.COM (void) put_nvlist(zc, errors); 227411022STom.Erickson@Sun.COM } 227511022STom.Erickson@Sun.COM 227611022STom.Erickson@Sun.COM nvlist_free(errors); 22772676Seschrock nvlist_free(nvl); 22782676Seschrock return (error); 2279789Sahrens } 2280789Sahrens 22815367Sahrens /* 22825367Sahrens * inputs: 22835367Sahrens * zc_name name of filesystem 22845367Sahrens * zc_value name of property to inherit 228511022STom.Erickson@Sun.COM * zc_cookie revert to received value if TRUE 22865367Sahrens * 22875367Sahrens * outputs: none 22885367Sahrens */ 2289789Sahrens static int 22904849Sahrens zfs_ioc_inherit_prop(zfs_cmd_t *zc) 22914849Sahrens { 229211022STom.Erickson@Sun.COM const char *propname = zc->zc_value; 229311022STom.Erickson@Sun.COM zfs_prop_t prop = zfs_name_to_prop(propname); 229411022STom.Erickson@Sun.COM boolean_t received = zc->zc_cookie; 229511022STom.Erickson@Sun.COM zprop_source_t source = (received 229611022STom.Erickson@Sun.COM ? ZPROP_SRC_NONE /* revert to received value, if any */ 229711022STom.Erickson@Sun.COM : ZPROP_SRC_INHERITED); /* explicitly inherit */ 229811022STom.Erickson@Sun.COM 229911022STom.Erickson@Sun.COM if (received) { 230011022STom.Erickson@Sun.COM nvlist_t *dummy; 230111022STom.Erickson@Sun.COM nvpair_t *pair; 230211022STom.Erickson@Sun.COM zprop_type_t type; 230311022STom.Erickson@Sun.COM int err; 230411022STom.Erickson@Sun.COM 230511022STom.Erickson@Sun.COM /* 230611022STom.Erickson@Sun.COM * zfs_prop_set_special() expects properties in the form of an 230711022STom.Erickson@Sun.COM * nvpair with type info. 230811022STom.Erickson@Sun.COM */ 230911022STom.Erickson@Sun.COM if (prop == ZPROP_INVAL) { 231011022STom.Erickson@Sun.COM if (!zfs_prop_user(propname)) 231111022STom.Erickson@Sun.COM return (EINVAL); 231211022STom.Erickson@Sun.COM 231311022STom.Erickson@Sun.COM type = PROP_TYPE_STRING; 231411515STom.Erickson@Sun.COM } else if (prop == ZFS_PROP_VOLSIZE || 231511515STom.Erickson@Sun.COM prop == ZFS_PROP_VERSION) { 231611515STom.Erickson@Sun.COM return (EINVAL); 231711022STom.Erickson@Sun.COM } else { 231811022STom.Erickson@Sun.COM type = zfs_prop_get_type(prop); 231911022STom.Erickson@Sun.COM } 232011022STom.Erickson@Sun.COM 232111022STom.Erickson@Sun.COM VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0); 232211022STom.Erickson@Sun.COM 232311022STom.Erickson@Sun.COM switch (type) { 232411022STom.Erickson@Sun.COM case PROP_TYPE_STRING: 232511022STom.Erickson@Sun.COM VERIFY(0 == nvlist_add_string(dummy, propname, "")); 232611022STom.Erickson@Sun.COM break; 232711022STom.Erickson@Sun.COM case PROP_TYPE_NUMBER: 232811022STom.Erickson@Sun.COM case PROP_TYPE_INDEX: 232911022STom.Erickson@Sun.COM VERIFY(0 == nvlist_add_uint64(dummy, propname, 0)); 233011022STom.Erickson@Sun.COM break; 233111022STom.Erickson@Sun.COM default: 233211022STom.Erickson@Sun.COM nvlist_free(dummy); 233311022STom.Erickson@Sun.COM return (EINVAL); 233411022STom.Erickson@Sun.COM } 233511022STom.Erickson@Sun.COM 233611022STom.Erickson@Sun.COM pair = nvlist_next_nvpair(dummy, NULL); 233711022STom.Erickson@Sun.COM err = zfs_prop_set_special(zc->zc_name, source, pair); 233811022STom.Erickson@Sun.COM nvlist_free(dummy); 233911022STom.Erickson@Sun.COM if (err != -1) 234011022STom.Erickson@Sun.COM return (err); /* special property already handled */ 234111022STom.Erickson@Sun.COM } else { 234211022STom.Erickson@Sun.COM /* 234311022STom.Erickson@Sun.COM * Only check this in the non-received case. We want to allow 234411022STom.Erickson@Sun.COM * 'inherit -S' to revert non-inheritable properties like quota 234511022STom.Erickson@Sun.COM * and reservation to the received or default values even though 234611022STom.Erickson@Sun.COM * they are not considered inheritable. 234711022STom.Erickson@Sun.COM */ 234811022STom.Erickson@Sun.COM if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop)) 234911022STom.Erickson@Sun.COM return (EINVAL); 235011022STom.Erickson@Sun.COM } 235111022STom.Erickson@Sun.COM 23524849Sahrens /* the property name has been validated by zfs_secpolicy_inherit() */ 235311022STom.Erickson@Sun.COM return (dsl_prop_set(zc->zc_name, zc->zc_value, source, 0, 0, NULL)); 23544849Sahrens } 23554849Sahrens 23564849Sahrens static int 23574098Slling zfs_ioc_pool_set_props(zfs_cmd_t *zc) 23583912Slling { 23595094Slling nvlist_t *props; 23603912Slling spa_t *spa; 23615094Slling int error; 236211022STom.Erickson@Sun.COM nvpair_t *pair; 236311022STom.Erickson@Sun.COM 236411022STom.Erickson@Sun.COM if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 236511022STom.Erickson@Sun.COM zc->zc_iflags, &props)) 23663912Slling return (error); 23673912Slling 23688525SEric.Schrock@Sun.COM /* 23698525SEric.Schrock@Sun.COM * If the only property is the configfile, then just do a spa_lookup() 23708525SEric.Schrock@Sun.COM * to handle the faulted case. 23718525SEric.Schrock@Sun.COM */ 237211022STom.Erickson@Sun.COM pair = nvlist_next_nvpair(props, NULL); 237311022STom.Erickson@Sun.COM if (pair != NULL && strcmp(nvpair_name(pair), 23748525SEric.Schrock@Sun.COM zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 && 237511022STom.Erickson@Sun.COM nvlist_next_nvpair(props, pair) == NULL) { 23768525SEric.Schrock@Sun.COM mutex_enter(&spa_namespace_lock); 23778525SEric.Schrock@Sun.COM if ((spa = spa_lookup(zc->zc_name)) != NULL) { 23788525SEric.Schrock@Sun.COM spa_configfile_set(spa, props, B_FALSE); 23798525SEric.Schrock@Sun.COM spa_config_sync(spa, B_FALSE, B_TRUE); 23808525SEric.Schrock@Sun.COM } 23818525SEric.Schrock@Sun.COM mutex_exit(&spa_namespace_lock); 238210672SEric.Schrock@Sun.COM if (spa != NULL) { 238310672SEric.Schrock@Sun.COM nvlist_free(props); 23848525SEric.Schrock@Sun.COM return (0); 238510672SEric.Schrock@Sun.COM } 23868525SEric.Schrock@Sun.COM } 23878525SEric.Schrock@Sun.COM 23883912Slling if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) { 23895094Slling nvlist_free(props); 23903912Slling return (error); 23913912Slling } 23923912Slling 23935094Slling error = spa_prop_set(spa, props); 23943912Slling 23955094Slling nvlist_free(props); 23963912Slling spa_close(spa, FTAG); 23973912Slling 23983912Slling return (error); 23993912Slling } 24003912Slling 24013912Slling static int 24024098Slling zfs_ioc_pool_get_props(zfs_cmd_t *zc) 24033912Slling { 24043912Slling spa_t *spa; 24053912Slling int error; 24063912Slling nvlist_t *nvp = NULL; 24073912Slling 24088525SEric.Schrock@Sun.COM if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) { 24098525SEric.Schrock@Sun.COM /* 24108525SEric.Schrock@Sun.COM * If the pool is faulted, there may be properties we can still 24118525SEric.Schrock@Sun.COM * get (such as altroot and cachefile), so attempt to get them 24128525SEric.Schrock@Sun.COM * anyway. 24138525SEric.Schrock@Sun.COM */ 24148525SEric.Schrock@Sun.COM mutex_enter(&spa_namespace_lock); 24158525SEric.Schrock@Sun.COM if ((spa = spa_lookup(zc->zc_name)) != NULL) 24168525SEric.Schrock@Sun.COM error = spa_prop_get(spa, &nvp); 24178525SEric.Schrock@Sun.COM mutex_exit(&spa_namespace_lock); 24188525SEric.Schrock@Sun.COM } else { 24198525SEric.Schrock@Sun.COM error = spa_prop_get(spa, &nvp); 24208525SEric.Schrock@Sun.COM spa_close(spa, FTAG); 24218525SEric.Schrock@Sun.COM } 24223912Slling 24233912Slling if (error == 0 && zc->zc_nvlist_dst != NULL) 24243912Slling error = put_nvlist(zc, nvp); 24253912Slling else 24263912Slling error = EFAULT; 24273912Slling 24288525SEric.Schrock@Sun.COM nvlist_free(nvp); 24293912Slling return (error); 24303912Slling } 24313912Slling 24325367Sahrens /* 24335367Sahrens * inputs: 24345367Sahrens * zc_name name of filesystem 24355367Sahrens * zc_nvlist_src{_size} nvlist of delegated permissions 24365367Sahrens * zc_perm_action allow/unallow flag 24375367Sahrens * 24385367Sahrens * outputs: none 24395367Sahrens */ 24404543Smarks static int 24414543Smarks zfs_ioc_set_fsacl(zfs_cmd_t *zc) 24424543Smarks { 24434543Smarks int error; 24444543Smarks nvlist_t *fsaclnv = NULL; 24454543Smarks 24465094Slling if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 24479643SEric.Taylor@Sun.COM zc->zc_iflags, &fsaclnv)) != 0) 24484543Smarks return (error); 24494543Smarks 24504543Smarks /* 24514543Smarks * Verify nvlist is constructed correctly 24524543Smarks */ 24534543Smarks if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) { 24544543Smarks nvlist_free(fsaclnv); 24554543Smarks return (EINVAL); 24564543Smarks } 24574543Smarks 24584543Smarks /* 24594543Smarks * If we don't have PRIV_SYS_MOUNT, then validate 24604543Smarks * that user is allowed to hand out each permission in 24614543Smarks * the nvlist(s) 24624543Smarks */ 24634543Smarks 24644787Sahrens error = secpolicy_zfs(CRED()); 24654543Smarks if (error) { 24664787Sahrens if (zc->zc_perm_action == B_FALSE) { 24674787Sahrens error = dsl_deleg_can_allow(zc->zc_name, 24684787Sahrens fsaclnv, CRED()); 24694787Sahrens } else { 24704787Sahrens error = dsl_deleg_can_unallow(zc->zc_name, 24714787Sahrens fsaclnv, CRED()); 24724787Sahrens } 24734543Smarks } 24744543Smarks 24754543Smarks if (error == 0) 24764543Smarks error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action); 24774543Smarks 24784543Smarks nvlist_free(fsaclnv); 24794543Smarks return (error); 24804543Smarks } 24814543Smarks 24825367Sahrens /* 24835367Sahrens * inputs: 24845367Sahrens * zc_name name of filesystem 24855367Sahrens * 24865367Sahrens * outputs: 24875367Sahrens * zc_nvlist_src{_size} nvlist of delegated permissions 24885367Sahrens */ 24894543Smarks static int 24904543Smarks zfs_ioc_get_fsacl(zfs_cmd_t *zc) 24914543Smarks { 24924543Smarks nvlist_t *nvp; 24934543Smarks int error; 24944543Smarks 24954543Smarks if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) { 24964543Smarks error = put_nvlist(zc, nvp); 24974543Smarks nvlist_free(nvp); 24984543Smarks } 24994543Smarks 25004543Smarks return (error); 25014543Smarks } 25024543Smarks 25035367Sahrens /* 2504789Sahrens * Search the vfs list for a specified resource. Returns a pointer to it 2505789Sahrens * or NULL if no suitable entry is found. The caller of this routine 2506789Sahrens * is responsible for releasing the returned vfs pointer. 2507789Sahrens */ 2508789Sahrens static vfs_t * 2509789Sahrens zfs_get_vfs(const char *resource) 2510789Sahrens { 2511789Sahrens struct vfs *vfsp; 2512789Sahrens struct vfs *vfs_found = NULL; 2513789Sahrens 2514789Sahrens vfs_list_read_lock(); 2515789Sahrens vfsp = rootvfs; 2516789Sahrens do { 2517789Sahrens if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) { 2518789Sahrens VFS_HOLD(vfsp); 2519789Sahrens vfs_found = vfsp; 2520789Sahrens break; 2521789Sahrens } 2522789Sahrens vfsp = vfsp->vfs_next; 2523789Sahrens } while (vfsp != rootvfs); 2524789Sahrens vfs_list_unlock(); 2525789Sahrens return (vfs_found); 2526789Sahrens } 2527789Sahrens 25284543Smarks /* ARGSUSED */ 2529789Sahrens static void 25304543Smarks zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx) 2531789Sahrens { 25325331Samw zfs_creat_t *zct = arg; 25335498Stimh 25345498Stimh zfs_create_fs(os, cr, zct->zct_zplprops, tx); 25355331Samw } 25365331Samw 25375498Stimh #define ZFS_PROP_UNDEFINED ((uint64_t)-1) 25385498Stimh 25395331Samw /* 25405498Stimh * inputs: 25417184Stimh * createprops list of properties requested by creator 25427184Stimh * default_zplver zpl version to use if unspecified in createprops 25437184Stimh * fuids_ok fuids allowed in this version of the spa? 25447184Stimh * os parent objset pointer (NULL if root fs) 25455331Samw * 25465498Stimh * outputs: 25475498Stimh * zplprops values for the zplprops we attach to the master node object 25487184Stimh * is_ci true if requested file system will be purely case-insensitive 25495331Samw * 25505498Stimh * Determine the settings for utf8only, normalization and 25515498Stimh * casesensitivity. Specific values may have been requested by the 25525498Stimh * creator and/or we can inherit values from the parent dataset. If 25535498Stimh * the file system is of too early a vintage, a creator can not 25545498Stimh * request settings for these properties, even if the requested 25555498Stimh * setting is the default value. We don't actually want to create dsl 25565498Stimh * properties for these, so remove them from the source nvlist after 25575498Stimh * processing. 25585331Samw */ 25595331Samw static int 25609396SMatthew.Ahrens@Sun.COM zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver, 25617184Stimh boolean_t fuids_ok, nvlist_t *createprops, nvlist_t *zplprops, 25627184Stimh boolean_t *is_ci) 25635331Samw { 25645498Stimh uint64_t sense = ZFS_PROP_UNDEFINED; 25655498Stimh uint64_t norm = ZFS_PROP_UNDEFINED; 25665498Stimh uint64_t u8 = ZFS_PROP_UNDEFINED; 25675498Stimh 25685498Stimh ASSERT(zplprops != NULL); 25695498Stimh 25705375Stimh /* 25715498Stimh * Pull out creator prop choices, if any. 25725375Stimh */ 25735498Stimh if (createprops) { 25745498Stimh (void) nvlist_lookup_uint64(createprops, 25757184Stimh zfs_prop_to_name(ZFS_PROP_VERSION), &zplver); 25767184Stimh (void) nvlist_lookup_uint64(createprops, 25775498Stimh zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm); 25785498Stimh (void) nvlist_remove_all(createprops, 25795498Stimh zfs_prop_to_name(ZFS_PROP_NORMALIZE)); 25805498Stimh (void) nvlist_lookup_uint64(createprops, 25815498Stimh zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8); 25825498Stimh (void) nvlist_remove_all(createprops, 25835498Stimh zfs_prop_to_name(ZFS_PROP_UTF8ONLY)); 25845498Stimh (void) nvlist_lookup_uint64(createprops, 25855498Stimh zfs_prop_to_name(ZFS_PROP_CASE), &sense); 25865498Stimh (void) nvlist_remove_all(createprops, 25875498Stimh zfs_prop_to_name(ZFS_PROP_CASE)); 25885331Samw } 25895331Samw 25905375Stimh /* 25917184Stimh * If the zpl version requested is whacky or the file system 25927184Stimh * or pool is version is too "young" to support normalization 25937184Stimh * and the creator tried to set a value for one of the props, 25947184Stimh * error out. 25955498Stimh */ 25967184Stimh if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) || 25977184Stimh (zplver >= ZPL_VERSION_FUID && !fuids_ok) || 25987184Stimh (zplver < ZPL_VERSION_NORMALIZATION && 25995498Stimh (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED || 26007184Stimh sense != ZFS_PROP_UNDEFINED))) 26015498Stimh return (ENOTSUP); 26025498Stimh 26035498Stimh /* 26045498Stimh * Put the version in the zplprops 26055498Stimh */ 26065498Stimh VERIFY(nvlist_add_uint64(zplprops, 26075498Stimh zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0); 26085498Stimh 26095498Stimh if (norm == ZFS_PROP_UNDEFINED) 26105498Stimh VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0); 26115498Stimh VERIFY(nvlist_add_uint64(zplprops, 26125498Stimh zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0); 26135498Stimh 26145498Stimh /* 26155498Stimh * If we're normalizing, names must always be valid UTF-8 strings. 26165498Stimh */ 26175498Stimh if (norm) 26185498Stimh u8 = 1; 26195498Stimh if (u8 == ZFS_PROP_UNDEFINED) 26205498Stimh VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0); 26215498Stimh VERIFY(nvlist_add_uint64(zplprops, 26225498Stimh zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0); 26235498Stimh 26245498Stimh if (sense == ZFS_PROP_UNDEFINED) 26255498Stimh VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0); 26265498Stimh VERIFY(nvlist_add_uint64(zplprops, 26275498Stimh zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0); 26285498Stimh 26296492Stimh if (is_ci) 26306492Stimh *is_ci = (sense == ZFS_CASE_INSENSITIVE); 26316492Stimh 26327184Stimh return (0); 26337184Stimh } 26347184Stimh 26357184Stimh static int 26367184Stimh zfs_fill_zplprops(const char *dataset, nvlist_t *createprops, 26377184Stimh nvlist_t *zplprops, boolean_t *is_ci) 26387184Stimh { 26397184Stimh boolean_t fuids_ok = B_TRUE; 26407184Stimh uint64_t zplver = ZPL_VERSION; 26417184Stimh objset_t *os = NULL; 26427184Stimh char parentname[MAXNAMELEN]; 26437184Stimh char *cp; 26447184Stimh int error; 26457184Stimh 26467184Stimh (void) strlcpy(parentname, dataset, sizeof (parentname)); 26477184Stimh cp = strrchr(parentname, '/'); 26487184Stimh ASSERT(cp != NULL); 26497184Stimh cp[0] = '\0'; 26507184Stimh 26519396SMatthew.Ahrens@Sun.COM if (zfs_earlier_version(dataset, SPA_VERSION_USERSPACE)) 26529396SMatthew.Ahrens@Sun.COM zplver = ZPL_VERSION_USERSPACE - 1; 26537184Stimh if (zfs_earlier_version(dataset, SPA_VERSION_FUID)) { 26547184Stimh zplver = ZPL_VERSION_FUID - 1; 26557184Stimh fuids_ok = B_FALSE; 26567184Stimh } 26577184Stimh 26587184Stimh /* 26597184Stimh * Open parent object set so we can inherit zplprop values. 26607184Stimh */ 266110298SMatthew.Ahrens@Sun.COM if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0) 26627184Stimh return (error); 26637184Stimh 26647184Stimh error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, createprops, 26657184Stimh zplprops, is_ci); 266610298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 26677184Stimh return (error); 26687184Stimh } 26697184Stimh 26707184Stimh static int 26717184Stimh zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops, 26727184Stimh nvlist_t *zplprops, boolean_t *is_ci) 26737184Stimh { 26747184Stimh boolean_t fuids_ok = B_TRUE; 26757184Stimh uint64_t zplver = ZPL_VERSION; 26767184Stimh int error; 26777184Stimh 26787184Stimh if (spa_vers < SPA_VERSION_FUID) { 26797184Stimh zplver = ZPL_VERSION_FUID - 1; 26807184Stimh fuids_ok = B_FALSE; 26817184Stimh } 26827184Stimh 26837184Stimh error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, createprops, 26847184Stimh zplprops, is_ci); 26857184Stimh return (error); 2686789Sahrens } 2687789Sahrens 26885367Sahrens /* 26895367Sahrens * inputs: 26905367Sahrens * zc_objset_type type of objset to create (fs vs zvol) 26915367Sahrens * zc_name name of new objset 26925367Sahrens * zc_value name of snapshot to clone from (may be empty) 26935367Sahrens * zc_nvlist_src{_size} nvlist of properties to apply 26945367Sahrens * 26955498Stimh * outputs: none 26965367Sahrens */ 2697789Sahrens static int 2698789Sahrens zfs_ioc_create(zfs_cmd_t *zc) 2699789Sahrens { 2700789Sahrens objset_t *clone; 2701789Sahrens int error = 0; 27025331Samw zfs_creat_t zct; 27034543Smarks nvlist_t *nvprops = NULL; 27044543Smarks void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx); 2705789Sahrens dmu_objset_type_t type = zc->zc_objset_type; 2706789Sahrens 2707789Sahrens switch (type) { 2708789Sahrens 2709789Sahrens case DMU_OST_ZFS: 2710789Sahrens cbfunc = zfs_create_cb; 2711789Sahrens break; 2712789Sahrens 2713789Sahrens case DMU_OST_ZVOL: 2714789Sahrens cbfunc = zvol_create_cb; 2715789Sahrens break; 2716789Sahrens 2717789Sahrens default: 27182199Sahrens cbfunc = NULL; 27196423Sgw25295 break; 27202199Sahrens } 27215326Sek110237 if (strchr(zc->zc_name, '@') || 27225326Sek110237 strchr(zc->zc_name, '%')) 2723789Sahrens return (EINVAL); 2724789Sahrens 27252676Seschrock if (zc->zc_nvlist_src != NULL && 27265094Slling (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 27279643SEric.Taylor@Sun.COM zc->zc_iflags, &nvprops)) != 0) 27282676Seschrock return (error); 27292676Seschrock 27305498Stimh zct.zct_zplprops = NULL; 27315331Samw zct.zct_props = nvprops; 27325331Samw 27332676Seschrock if (zc->zc_value[0] != '\0') { 2734789Sahrens /* 2735789Sahrens * We're creating a clone of an existing snapshot. 2736789Sahrens */ 27372676Seschrock zc->zc_value[sizeof (zc->zc_value) - 1] = '\0'; 27382676Seschrock if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) { 27394543Smarks nvlist_free(nvprops); 2740789Sahrens return (EINVAL); 27412676Seschrock } 2742789Sahrens 274310298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(zc->zc_value, FTAG, &clone); 27442676Seschrock if (error) { 27454543Smarks nvlist_free(nvprops); 2746789Sahrens return (error); 27472676Seschrock } 27486492Stimh 274910272SMatthew.Ahrens@Sun.COM error = dmu_objset_clone(zc->zc_name, dmu_objset_ds(clone), 0); 275010298SMatthew.Ahrens@Sun.COM dmu_objset_rele(clone, FTAG); 27515331Samw if (error) { 27525331Samw nvlist_free(nvprops); 27535331Samw return (error); 27545331Samw } 2755789Sahrens } else { 27566492Stimh boolean_t is_insensitive = B_FALSE; 27576492Stimh 27582676Seschrock if (cbfunc == NULL) { 27594543Smarks nvlist_free(nvprops); 27602199Sahrens return (EINVAL); 27612676Seschrock } 27622676Seschrock 2763789Sahrens if (type == DMU_OST_ZVOL) { 27642676Seschrock uint64_t volsize, volblocksize; 27652676Seschrock 27664543Smarks if (nvprops == NULL || 27674543Smarks nvlist_lookup_uint64(nvprops, 27682676Seschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), 27692676Seschrock &volsize) != 0) { 27704543Smarks nvlist_free(nvprops); 27712676Seschrock return (EINVAL); 27722676Seschrock } 27732676Seschrock 27744543Smarks if ((error = nvlist_lookup_uint64(nvprops, 27752676Seschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 27762676Seschrock &volblocksize)) != 0 && error != ENOENT) { 27774543Smarks nvlist_free(nvprops); 27782676Seschrock return (EINVAL); 27792676Seschrock } 27801133Seschrock 27812676Seschrock if (error != 0) 27822676Seschrock volblocksize = zfs_prop_default_numeric( 27832676Seschrock ZFS_PROP_VOLBLOCKSIZE); 27842676Seschrock 27852676Seschrock if ((error = zvol_check_volblocksize( 27862676Seschrock volblocksize)) != 0 || 27872676Seschrock (error = zvol_check_volsize(volsize, 27882676Seschrock volblocksize)) != 0) { 27894543Smarks nvlist_free(nvprops); 2790789Sahrens return (error); 27912676Seschrock } 27924577Sahrens } else if (type == DMU_OST_ZFS) { 27935331Samw int error; 27945331Samw 27955498Stimh /* 27965331Samw * We have to have normalization and 27975331Samw * case-folding flags correct when we do the 27985331Samw * file system creation, so go figure them out 27995498Stimh * now. 28005331Samw */ 28015498Stimh VERIFY(nvlist_alloc(&zct.zct_zplprops, 28025498Stimh NV_UNIQUE_NAME, KM_SLEEP) == 0); 28035498Stimh error = zfs_fill_zplprops(zc->zc_name, nvprops, 28047184Stimh zct.zct_zplprops, &is_insensitive); 28055331Samw if (error != 0) { 28065331Samw nvlist_free(nvprops); 28075498Stimh nvlist_free(zct.zct_zplprops); 28085331Samw return (error); 28094577Sahrens } 28102676Seschrock } 281110272SMatthew.Ahrens@Sun.COM error = dmu_objset_create(zc->zc_name, type, 28126492Stimh is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct); 28135498Stimh nvlist_free(zct.zct_zplprops); 2814789Sahrens } 28152676Seschrock 28162676Seschrock /* 28172676Seschrock * It would be nice to do this atomically. 28182676Seschrock */ 28192676Seschrock if (error == 0) { 282011022STom.Erickson@Sun.COM error = zfs_set_prop_nvlist(zc->zc_name, ZPROP_SRC_LOCAL, 282111022STom.Erickson@Sun.COM nvprops, NULL); 282211022STom.Erickson@Sun.COM if (error != 0) 282310242Schris.kirby@sun.com (void) dmu_objset_destroy(zc->zc_name, B_FALSE); 28242676Seschrock } 28254543Smarks nvlist_free(nvprops); 2826789Sahrens return (error); 2827789Sahrens } 2828789Sahrens 28295367Sahrens /* 28305367Sahrens * inputs: 28315367Sahrens * zc_name name of filesystem 28325367Sahrens * zc_value short name of snapshot 28335367Sahrens * zc_cookie recursive flag 28349396SMatthew.Ahrens@Sun.COM * zc_nvlist_src[_size] property list 28355367Sahrens * 283610588SEric.Taylor@Sun.COM * outputs: 283710588SEric.Taylor@Sun.COM * zc_value short snapname (i.e. part after the '@') 28385367Sahrens */ 2839789Sahrens static int 28402199Sahrens zfs_ioc_snapshot(zfs_cmd_t *zc) 28412199Sahrens { 28427265Sahrens nvlist_t *nvprops = NULL; 28437265Sahrens int error; 28447265Sahrens boolean_t recursive = zc->zc_cookie; 28457265Sahrens 28462676Seschrock if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0) 28472199Sahrens return (EINVAL); 28487265Sahrens 28497265Sahrens if (zc->zc_nvlist_src != NULL && 28507265Sahrens (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 28519643SEric.Taylor@Sun.COM zc->zc_iflags, &nvprops)) != 0) 28527265Sahrens return (error); 28537265Sahrens 28549355SMatthew.Ahrens@Sun.COM error = zfs_check_userprops(zc->zc_name, nvprops); 28559355SMatthew.Ahrens@Sun.COM if (error) 28569355SMatthew.Ahrens@Sun.COM goto out; 28579355SMatthew.Ahrens@Sun.COM 285811022STom.Erickson@Sun.COM if (!nvlist_empty(nvprops) && 28599355SMatthew.Ahrens@Sun.COM zfs_earlier_version(zc->zc_name, SPA_VERSION_SNAP_PROPS)) { 28609355SMatthew.Ahrens@Sun.COM error = ENOTSUP; 28619355SMatthew.Ahrens@Sun.COM goto out; 28627265Sahrens } 28639355SMatthew.Ahrens@Sun.COM 28649355SMatthew.Ahrens@Sun.COM error = dmu_objset_snapshot(zc->zc_name, zc->zc_value, 28659355SMatthew.Ahrens@Sun.COM nvprops, recursive); 28669355SMatthew.Ahrens@Sun.COM 28679355SMatthew.Ahrens@Sun.COM out: 28687265Sahrens nvlist_free(nvprops); 28697265Sahrens return (error); 28702199Sahrens } 28712199Sahrens 28724007Smmusante int 287311209SMatthew.Ahrens@Sun.COM zfs_unmount_snap(const char *name, void *arg) 2874789Sahrens { 28752417Sahrens vfs_t *vfsp = NULL; 28762199Sahrens 28776689Smaybee if (arg) { 28786689Smaybee char *snapname = arg; 287911209SMatthew.Ahrens@Sun.COM char *fullname = kmem_asprintf("%s@%s", name, snapname); 288011209SMatthew.Ahrens@Sun.COM vfsp = zfs_get_vfs(fullname); 288111209SMatthew.Ahrens@Sun.COM strfree(fullname); 28822417Sahrens } else if (strchr(name, '@')) { 28832199Sahrens vfsp = zfs_get_vfs(name); 28842199Sahrens } 28852199Sahrens 28862199Sahrens if (vfsp) { 28872199Sahrens /* 28882199Sahrens * Always force the unmount for snapshots. 28892199Sahrens */ 28902199Sahrens int flag = MS_FORCE; 2891789Sahrens int err; 2892789Sahrens 28932199Sahrens if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) { 28942199Sahrens VFS_RELE(vfsp); 28952199Sahrens return (err); 28962199Sahrens } 28972199Sahrens VFS_RELE(vfsp); 28982199Sahrens if ((err = dounmount(vfsp, flag, kcred)) != 0) 28992199Sahrens return (err); 29002199Sahrens } 29012199Sahrens return (0); 29022199Sahrens } 29032199Sahrens 29045367Sahrens /* 29055367Sahrens * inputs: 290610242Schris.kirby@sun.com * zc_name name of filesystem 290710242Schris.kirby@sun.com * zc_value short name of snapshot 290810242Schris.kirby@sun.com * zc_defer_destroy mark for deferred destroy 29095367Sahrens * 29105367Sahrens * outputs: none 29115367Sahrens */ 29122199Sahrens static int 29132199Sahrens zfs_ioc_destroy_snaps(zfs_cmd_t *zc) 29142199Sahrens { 29152199Sahrens int err; 2916789Sahrens 29172676Seschrock if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0) 29182199Sahrens return (EINVAL); 29192199Sahrens err = dmu_objset_find(zc->zc_name, 29202676Seschrock zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN); 29212199Sahrens if (err) 29222199Sahrens return (err); 292310242Schris.kirby@sun.com return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value, 292410242Schris.kirby@sun.com zc->zc_defer_destroy)); 29252199Sahrens } 29262199Sahrens 29275367Sahrens /* 29285367Sahrens * inputs: 29295367Sahrens * zc_name name of dataset to destroy 29305367Sahrens * zc_objset_type type of objset 293110242Schris.kirby@sun.com * zc_defer_destroy mark for deferred destroy 29325367Sahrens * 29335367Sahrens * outputs: none 29345367Sahrens */ 29352199Sahrens static int 29362199Sahrens zfs_ioc_destroy(zfs_cmd_t *zc) 29372199Sahrens { 293810588SEric.Taylor@Sun.COM int err; 29392199Sahrens if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) { 294010588SEric.Taylor@Sun.COM err = zfs_unmount_snap(zc->zc_name, NULL); 29412199Sahrens if (err) 29422199Sahrens return (err); 2943789Sahrens } 2944789Sahrens 294510588SEric.Taylor@Sun.COM err = dmu_objset_destroy(zc->zc_name, zc->zc_defer_destroy); 294610588SEric.Taylor@Sun.COM if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0) 294710693Schris.kirby@sun.com (void) zvol_remove_minor(zc->zc_name); 294810588SEric.Taylor@Sun.COM return (err); 2949789Sahrens } 2950789Sahrens 29515367Sahrens /* 29525367Sahrens * inputs: 29535446Sahrens * zc_name name of dataset to rollback (to most recent snapshot) 29545367Sahrens * 29555367Sahrens * outputs: none 29565367Sahrens */ 2957789Sahrens static int 2958789Sahrens zfs_ioc_rollback(zfs_cmd_t *zc) 2959789Sahrens { 296010272SMatthew.Ahrens@Sun.COM dsl_dataset_t *ds, *clone; 29615446Sahrens int error; 296210272SMatthew.Ahrens@Sun.COM zfsvfs_t *zfsvfs; 296310272SMatthew.Ahrens@Sun.COM char *clone_name; 296410272SMatthew.Ahrens@Sun.COM 296510272SMatthew.Ahrens@Sun.COM error = dsl_dataset_hold(zc->zc_name, FTAG, &ds); 296610272SMatthew.Ahrens@Sun.COM if (error) 296710272SMatthew.Ahrens@Sun.COM return (error); 296810272SMatthew.Ahrens@Sun.COM 296910272SMatthew.Ahrens@Sun.COM /* must not be a snapshot */ 297010272SMatthew.Ahrens@Sun.COM if (dsl_dataset_is_snapshot(ds)) { 297110272SMatthew.Ahrens@Sun.COM dsl_dataset_rele(ds, FTAG); 297210272SMatthew.Ahrens@Sun.COM return (EINVAL); 297310272SMatthew.Ahrens@Sun.COM } 297410272SMatthew.Ahrens@Sun.COM 297510272SMatthew.Ahrens@Sun.COM /* must have a most recent snapshot */ 297610272SMatthew.Ahrens@Sun.COM if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) { 297710272SMatthew.Ahrens@Sun.COM dsl_dataset_rele(ds, FTAG); 297810272SMatthew.Ahrens@Sun.COM return (EINVAL); 297910272SMatthew.Ahrens@Sun.COM } 29805446Sahrens 29815446Sahrens /* 298210272SMatthew.Ahrens@Sun.COM * Create clone of most recent snapshot. 29835446Sahrens */ 298410272SMatthew.Ahrens@Sun.COM clone_name = kmem_asprintf("%s/%%rollback", zc->zc_name); 298510272SMatthew.Ahrens@Sun.COM error = dmu_objset_clone(clone_name, ds->ds_prev, DS_FLAG_INCONSISTENT); 29865446Sahrens if (error) 298710272SMatthew.Ahrens@Sun.COM goto out; 298810272SMatthew.Ahrens@Sun.COM 298910298SMatthew.Ahrens@Sun.COM error = dsl_dataset_own(clone_name, B_TRUE, FTAG, &clone); 299010272SMatthew.Ahrens@Sun.COM if (error) 299110272SMatthew.Ahrens@Sun.COM goto out; 299210272SMatthew.Ahrens@Sun.COM 299310272SMatthew.Ahrens@Sun.COM /* 299410272SMatthew.Ahrens@Sun.COM * Do clone swap. 299510272SMatthew.Ahrens@Sun.COM */ 29969396SMatthew.Ahrens@Sun.COM if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) { 299710298SMatthew.Ahrens@Sun.COM error = zfs_suspend_fs(zfsvfs); 29986083Sek110237 if (error == 0) { 29996083Sek110237 int resume_err; 30006083Sek110237 300110272SMatthew.Ahrens@Sun.COM if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) { 300210272SMatthew.Ahrens@Sun.COM error = dsl_dataset_clone_swap(clone, ds, 300310272SMatthew.Ahrens@Sun.COM B_TRUE); 300410272SMatthew.Ahrens@Sun.COM dsl_dataset_disown(ds, FTAG); 300510272SMatthew.Ahrens@Sun.COM ds = NULL; 300610272SMatthew.Ahrens@Sun.COM } else { 300710272SMatthew.Ahrens@Sun.COM error = EBUSY; 300810272SMatthew.Ahrens@Sun.COM } 300910298SMatthew.Ahrens@Sun.COM resume_err = zfs_resume_fs(zfsvfs, zc->zc_name); 30106083Sek110237 error = error ? error : resume_err; 30116083Sek110237 } 30125446Sahrens VFS_RELE(zfsvfs->z_vfs); 30135446Sahrens } else { 301410272SMatthew.Ahrens@Sun.COM if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) { 301510272SMatthew.Ahrens@Sun.COM error = dsl_dataset_clone_swap(clone, ds, B_TRUE); 301610272SMatthew.Ahrens@Sun.COM dsl_dataset_disown(ds, FTAG); 301710272SMatthew.Ahrens@Sun.COM ds = NULL; 301810272SMatthew.Ahrens@Sun.COM } else { 301910272SMatthew.Ahrens@Sun.COM error = EBUSY; 302010272SMatthew.Ahrens@Sun.COM } 30215446Sahrens } 302210272SMatthew.Ahrens@Sun.COM 302310272SMatthew.Ahrens@Sun.COM /* 302410272SMatthew.Ahrens@Sun.COM * Destroy clone (which also closes it). 302510272SMatthew.Ahrens@Sun.COM */ 302610272SMatthew.Ahrens@Sun.COM (void) dsl_dataset_destroy(clone, FTAG, B_FALSE); 302710272SMatthew.Ahrens@Sun.COM 302810272SMatthew.Ahrens@Sun.COM out: 302910272SMatthew.Ahrens@Sun.COM strfree(clone_name); 303010272SMatthew.Ahrens@Sun.COM if (ds) 303110272SMatthew.Ahrens@Sun.COM dsl_dataset_rele(ds, FTAG); 30325446Sahrens return (error); 3033789Sahrens } 3034789Sahrens 30355367Sahrens /* 30365367Sahrens * inputs: 30375367Sahrens * zc_name old name of dataset 30385367Sahrens * zc_value new name of dataset 30395367Sahrens * zc_cookie recursive flag (only valid for snapshots) 30405367Sahrens * 30415367Sahrens * outputs: none 30425367Sahrens */ 3043789Sahrens static int 3044789Sahrens zfs_ioc_rename(zfs_cmd_t *zc) 3045789Sahrens { 30464490Svb160487 boolean_t recursive = zc->zc_cookie & 1; 30474007Smmusante 30482676Seschrock zc->zc_value[sizeof (zc->zc_value) - 1] = '\0'; 30495326Sek110237 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 || 30505326Sek110237 strchr(zc->zc_value, '%')) 3051789Sahrens return (EINVAL); 3052789Sahrens 30534007Smmusante /* 30544007Smmusante * Unmount snapshot unless we're doing a recursive rename, 30554007Smmusante * in which case the dataset code figures out which snapshots 30564007Smmusante * to unmount. 30574007Smmusante */ 30584007Smmusante if (!recursive && strchr(zc->zc_name, '@') != NULL && 3059789Sahrens zc->zc_objset_type == DMU_OST_ZFS) { 30602199Sahrens int err = zfs_unmount_snap(zc->zc_name, NULL); 30612199Sahrens if (err) 30622199Sahrens return (err); 3063789Sahrens } 306410588SEric.Taylor@Sun.COM if (zc->zc_objset_type == DMU_OST_ZVOL) 306510588SEric.Taylor@Sun.COM (void) zvol_remove_minor(zc->zc_name); 30664007Smmusante return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive)); 3067789Sahrens } 3068789Sahrens 306911022STom.Erickson@Sun.COM static int 307011022STom.Erickson@Sun.COM zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr) 307111022STom.Erickson@Sun.COM { 307211022STom.Erickson@Sun.COM const char *propname = nvpair_name(pair); 307311022STom.Erickson@Sun.COM boolean_t issnap = (strchr(dsname, '@') != NULL); 307411022STom.Erickson@Sun.COM zfs_prop_t prop = zfs_name_to_prop(propname); 307511022STom.Erickson@Sun.COM uint64_t intval; 307611022STom.Erickson@Sun.COM int err; 307711022STom.Erickson@Sun.COM 307811022STom.Erickson@Sun.COM if (prop == ZPROP_INVAL) { 307911022STom.Erickson@Sun.COM if (zfs_prop_user(propname)) { 308011022STom.Erickson@Sun.COM if (err = zfs_secpolicy_write_perms(dsname, 308111022STom.Erickson@Sun.COM ZFS_DELEG_PERM_USERPROP, cr)) 308211022STom.Erickson@Sun.COM return (err); 308311022STom.Erickson@Sun.COM return (0); 308411022STom.Erickson@Sun.COM } 308511022STom.Erickson@Sun.COM 308611022STom.Erickson@Sun.COM if (!issnap && zfs_prop_userquota(propname)) { 308711022STom.Erickson@Sun.COM const char *perm = NULL; 308811022STom.Erickson@Sun.COM const char *uq_prefix = 308911022STom.Erickson@Sun.COM zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA]; 309011022STom.Erickson@Sun.COM const char *gq_prefix = 309111022STom.Erickson@Sun.COM zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA]; 309211022STom.Erickson@Sun.COM 309311022STom.Erickson@Sun.COM if (strncmp(propname, uq_prefix, 309411022STom.Erickson@Sun.COM strlen(uq_prefix)) == 0) { 309511022STom.Erickson@Sun.COM perm = ZFS_DELEG_PERM_USERQUOTA; 309611022STom.Erickson@Sun.COM } else if (strncmp(propname, gq_prefix, 309711022STom.Erickson@Sun.COM strlen(gq_prefix)) == 0) { 309811022STom.Erickson@Sun.COM perm = ZFS_DELEG_PERM_GROUPQUOTA; 309911022STom.Erickson@Sun.COM } else { 310011022STom.Erickson@Sun.COM /* USERUSED and GROUPUSED are read-only */ 310111022STom.Erickson@Sun.COM return (EINVAL); 310211022STom.Erickson@Sun.COM } 310311022STom.Erickson@Sun.COM 310411022STom.Erickson@Sun.COM if (err = zfs_secpolicy_write_perms(dsname, perm, cr)) 310511022STom.Erickson@Sun.COM return (err); 310611022STom.Erickson@Sun.COM return (0); 310711022STom.Erickson@Sun.COM } 310811022STom.Erickson@Sun.COM 310911022STom.Erickson@Sun.COM return (EINVAL); 311011022STom.Erickson@Sun.COM } 311111022STom.Erickson@Sun.COM 311211022STom.Erickson@Sun.COM if (issnap) 311311022STom.Erickson@Sun.COM return (EINVAL); 311411022STom.Erickson@Sun.COM 311511022STom.Erickson@Sun.COM if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 311611022STom.Erickson@Sun.COM /* 311711022STom.Erickson@Sun.COM * dsl_prop_get_all_impl() returns properties in this 311811022STom.Erickson@Sun.COM * format. 311911022STom.Erickson@Sun.COM */ 312011022STom.Erickson@Sun.COM nvlist_t *attrs; 312111022STom.Erickson@Sun.COM VERIFY(nvpair_value_nvlist(pair, &attrs) == 0); 312211022STom.Erickson@Sun.COM VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 312311022STom.Erickson@Sun.COM &pair) == 0); 312411022STom.Erickson@Sun.COM } 312511022STom.Erickson@Sun.COM 312611022STom.Erickson@Sun.COM /* 312711022STom.Erickson@Sun.COM * Check that this value is valid for this pool version 312811022STom.Erickson@Sun.COM */ 312911022STom.Erickson@Sun.COM switch (prop) { 313011022STom.Erickson@Sun.COM case ZFS_PROP_COMPRESSION: 313111022STom.Erickson@Sun.COM /* 313211022STom.Erickson@Sun.COM * If the user specified gzip compression, make sure 313311022STom.Erickson@Sun.COM * the SPA supports it. We ignore any errors here since 313411022STom.Erickson@Sun.COM * we'll catch them later. 313511022STom.Erickson@Sun.COM */ 313611022STom.Erickson@Sun.COM if (nvpair_type(pair) == DATA_TYPE_UINT64 && 313711022STom.Erickson@Sun.COM nvpair_value_uint64(pair, &intval) == 0) { 313811022STom.Erickson@Sun.COM if (intval >= ZIO_COMPRESS_GZIP_1 && 313911022STom.Erickson@Sun.COM intval <= ZIO_COMPRESS_GZIP_9 && 314011022STom.Erickson@Sun.COM zfs_earlier_version(dsname, 314111022STom.Erickson@Sun.COM SPA_VERSION_GZIP_COMPRESSION)) { 314211022STom.Erickson@Sun.COM return (ENOTSUP); 314311022STom.Erickson@Sun.COM } 314411022STom.Erickson@Sun.COM 314511022STom.Erickson@Sun.COM if (intval == ZIO_COMPRESS_ZLE && 314611022STom.Erickson@Sun.COM zfs_earlier_version(dsname, 314711022STom.Erickson@Sun.COM SPA_VERSION_ZLE_COMPRESSION)) 314811022STom.Erickson@Sun.COM return (ENOTSUP); 314911022STom.Erickson@Sun.COM 315011022STom.Erickson@Sun.COM /* 315111022STom.Erickson@Sun.COM * If this is a bootable dataset then 315211022STom.Erickson@Sun.COM * verify that the compression algorithm 315311022STom.Erickson@Sun.COM * is supported for booting. We must return 315411022STom.Erickson@Sun.COM * something other than ENOTSUP since it 315511022STom.Erickson@Sun.COM * implies a downrev pool version. 315611022STom.Erickson@Sun.COM */ 315711022STom.Erickson@Sun.COM if (zfs_is_bootfs(dsname) && 315811022STom.Erickson@Sun.COM !BOOTFS_COMPRESS_VALID(intval)) { 315911022STom.Erickson@Sun.COM return (ERANGE); 316011022STom.Erickson@Sun.COM } 316111022STom.Erickson@Sun.COM } 316211022STom.Erickson@Sun.COM break; 316311022STom.Erickson@Sun.COM 316411022STom.Erickson@Sun.COM case ZFS_PROP_COPIES: 316511022STom.Erickson@Sun.COM if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS)) 316611022STom.Erickson@Sun.COM return (ENOTSUP); 316711022STom.Erickson@Sun.COM break; 316811022STom.Erickson@Sun.COM 316911022STom.Erickson@Sun.COM case ZFS_PROP_DEDUP: 317011022STom.Erickson@Sun.COM if (zfs_earlier_version(dsname, SPA_VERSION_DEDUP)) 317111022STom.Erickson@Sun.COM return (ENOTSUP); 317211022STom.Erickson@Sun.COM break; 317311022STom.Erickson@Sun.COM 317411022STom.Erickson@Sun.COM case ZFS_PROP_SHARESMB: 317511022STom.Erickson@Sun.COM if (zpl_earlier_version(dsname, ZPL_VERSION_FUID)) 317611022STom.Erickson@Sun.COM return (ENOTSUP); 317711022STom.Erickson@Sun.COM break; 317811022STom.Erickson@Sun.COM 317911022STom.Erickson@Sun.COM case ZFS_PROP_ACLINHERIT: 318011022STom.Erickson@Sun.COM if (nvpair_type(pair) == DATA_TYPE_UINT64 && 318111022STom.Erickson@Sun.COM nvpair_value_uint64(pair, &intval) == 0) { 318211022STom.Erickson@Sun.COM if (intval == ZFS_ACL_PASSTHROUGH_X && 318311022STom.Erickson@Sun.COM zfs_earlier_version(dsname, 318411022STom.Erickson@Sun.COM SPA_VERSION_PASSTHROUGH_X)) 318511022STom.Erickson@Sun.COM return (ENOTSUP); 318611022STom.Erickson@Sun.COM } 318711022STom.Erickson@Sun.COM break; 318811022STom.Erickson@Sun.COM } 318911022STom.Erickson@Sun.COM 319011022STom.Erickson@Sun.COM return (zfs_secpolicy_setprop(dsname, prop, pair, CRED())); 319111022STom.Erickson@Sun.COM } 319211022STom.Erickson@Sun.COM 319311022STom.Erickson@Sun.COM /* 319411022STom.Erickson@Sun.COM * Removes properties from the given props list that fail permission checks 319511022STom.Erickson@Sun.COM * needed to clear them and to restore them in case of a receive error. For each 319611022STom.Erickson@Sun.COM * property, make sure we have both set and inherit permissions. 319711022STom.Erickson@Sun.COM * 319811022STom.Erickson@Sun.COM * Returns the first error encountered if any permission checks fail. If the 319911022STom.Erickson@Sun.COM * caller provides a non-NULL errlist, it also gives the complete list of names 320011022STom.Erickson@Sun.COM * of all the properties that failed a permission check along with the 320111022STom.Erickson@Sun.COM * corresponding error numbers. The caller is responsible for freeing the 320211022STom.Erickson@Sun.COM * returned errlist. 320311022STom.Erickson@Sun.COM * 320411022STom.Erickson@Sun.COM * If every property checks out successfully, zero is returned and the list 320511022STom.Erickson@Sun.COM * pointed at by errlist is NULL. 320611022STom.Erickson@Sun.COM */ 320711022STom.Erickson@Sun.COM static int 320811022STom.Erickson@Sun.COM zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist) 32096689Smaybee { 32106689Smaybee zfs_cmd_t *zc; 321111022STom.Erickson@Sun.COM nvpair_t *pair, *next_pair; 321211022STom.Erickson@Sun.COM nvlist_t *errors; 321311022STom.Erickson@Sun.COM int err, rv = 0; 32146689Smaybee 32156689Smaybee if (props == NULL) 321611022STom.Erickson@Sun.COM return (0); 321711022STom.Erickson@Sun.COM 321811022STom.Erickson@Sun.COM VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0); 321911022STom.Erickson@Sun.COM 32206689Smaybee zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP); 32216689Smaybee (void) strcpy(zc->zc_name, dataset); 322211022STom.Erickson@Sun.COM pair = nvlist_next_nvpair(props, NULL); 322311022STom.Erickson@Sun.COM while (pair != NULL) { 322411022STom.Erickson@Sun.COM next_pair = nvlist_next_nvpair(props, pair); 322511022STom.Erickson@Sun.COM 322611022STom.Erickson@Sun.COM (void) strcpy(zc->zc_value, nvpair_name(pair)); 322711022STom.Erickson@Sun.COM if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 || 322811022STom.Erickson@Sun.COM (err = zfs_secpolicy_inherit(zc, CRED())) != 0) { 322911022STom.Erickson@Sun.COM VERIFY(nvlist_remove_nvpair(props, pair) == 0); 323011022STom.Erickson@Sun.COM VERIFY(nvlist_add_int32(errors, 323111022STom.Erickson@Sun.COM zc->zc_value, err) == 0); 323211022STom.Erickson@Sun.COM } 323311022STom.Erickson@Sun.COM pair = next_pair; 32346689Smaybee } 32356689Smaybee kmem_free(zc, sizeof (zfs_cmd_t)); 323611022STom.Erickson@Sun.COM 323711022STom.Erickson@Sun.COM if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) { 323811022STom.Erickson@Sun.COM nvlist_free(errors); 323911022STom.Erickson@Sun.COM errors = NULL; 324011022STom.Erickson@Sun.COM } else { 324111022STom.Erickson@Sun.COM VERIFY(nvpair_value_int32(pair, &rv) == 0); 324211022STom.Erickson@Sun.COM } 324311022STom.Erickson@Sun.COM 324411022STom.Erickson@Sun.COM if (errlist == NULL) 324511022STom.Erickson@Sun.COM nvlist_free(errors); 324611022STom.Erickson@Sun.COM else 324711022STom.Erickson@Sun.COM *errlist = errors; 324811022STom.Erickson@Sun.COM 324911022STom.Erickson@Sun.COM return (rv); 32506689Smaybee } 32516689Smaybee 325211022STom.Erickson@Sun.COM static boolean_t 325311022STom.Erickson@Sun.COM propval_equals(nvpair_t *p1, nvpair_t *p2) 325411022STom.Erickson@Sun.COM { 325511022STom.Erickson@Sun.COM if (nvpair_type(p1) == DATA_TYPE_NVLIST) { 325611022STom.Erickson@Sun.COM /* dsl_prop_get_all_impl() format */ 325711022STom.Erickson@Sun.COM nvlist_t *attrs; 325811022STom.Erickson@Sun.COM VERIFY(nvpair_value_nvlist(p1, &attrs) == 0); 325911022STom.Erickson@Sun.COM VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 326011022STom.Erickson@Sun.COM &p1) == 0); 326111022STom.Erickson@Sun.COM } 326211022STom.Erickson@Sun.COM 326311022STom.Erickson@Sun.COM if (nvpair_type(p2) == DATA_TYPE_NVLIST) { 326411022STom.Erickson@Sun.COM nvlist_t *attrs; 326511022STom.Erickson@Sun.COM VERIFY(nvpair_value_nvlist(p2, &attrs) == 0); 326611022STom.Erickson@Sun.COM VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 326711022STom.Erickson@Sun.COM &p2) == 0); 326811022STom.Erickson@Sun.COM } 326911022STom.Erickson@Sun.COM 327011022STom.Erickson@Sun.COM if (nvpair_type(p1) != nvpair_type(p2)) 327111022STom.Erickson@Sun.COM return (B_FALSE); 327211022STom.Erickson@Sun.COM 327311022STom.Erickson@Sun.COM if (nvpair_type(p1) == DATA_TYPE_STRING) { 327411022STom.Erickson@Sun.COM char *valstr1, *valstr2; 327511022STom.Erickson@Sun.COM 327611022STom.Erickson@Sun.COM VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0); 327711022STom.Erickson@Sun.COM VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0); 327811022STom.Erickson@Sun.COM return (strcmp(valstr1, valstr2) == 0); 327911022STom.Erickson@Sun.COM } else { 328011022STom.Erickson@Sun.COM uint64_t intval1, intval2; 328111022STom.Erickson@Sun.COM 328211022STom.Erickson@Sun.COM VERIFY(nvpair_value_uint64(p1, &intval1) == 0); 328311022STom.Erickson@Sun.COM VERIFY(nvpair_value_uint64(p2, &intval2) == 0); 328411022STom.Erickson@Sun.COM return (intval1 == intval2); 328511022STom.Erickson@Sun.COM } 328611022STom.Erickson@Sun.COM } 328711022STom.Erickson@Sun.COM 328811022STom.Erickson@Sun.COM /* 328911022STom.Erickson@Sun.COM * Remove properties from props if they are not going to change (as determined 329011022STom.Erickson@Sun.COM * by comparison with origprops). Remove them from origprops as well, since we 329111022STom.Erickson@Sun.COM * do not need to clear or restore properties that won't change. 329211022STom.Erickson@Sun.COM */ 329311022STom.Erickson@Sun.COM static void 329411022STom.Erickson@Sun.COM props_reduce(nvlist_t *props, nvlist_t *origprops) 329511022STom.Erickson@Sun.COM { 329611022STom.Erickson@Sun.COM nvpair_t *pair, *next_pair; 329711022STom.Erickson@Sun.COM 329811022STom.Erickson@Sun.COM if (origprops == NULL) 329911022STom.Erickson@Sun.COM return; /* all props need to be received */ 330011022STom.Erickson@Sun.COM 330111022STom.Erickson@Sun.COM pair = nvlist_next_nvpair(props, NULL); 330211022STom.Erickson@Sun.COM while (pair != NULL) { 330311022STom.Erickson@Sun.COM const char *propname = nvpair_name(pair); 330411022STom.Erickson@Sun.COM nvpair_t *match; 330511022STom.Erickson@Sun.COM 330611022STom.Erickson@Sun.COM next_pair = nvlist_next_nvpair(props, pair); 330711022STom.Erickson@Sun.COM 330811022STom.Erickson@Sun.COM if ((nvlist_lookup_nvpair(origprops, propname, 330911022STom.Erickson@Sun.COM &match) != 0) || !propval_equals(pair, match)) 331011022STom.Erickson@Sun.COM goto next; /* need to set received value */ 331111022STom.Erickson@Sun.COM 331211022STom.Erickson@Sun.COM /* don't clear the existing received value */ 331311022STom.Erickson@Sun.COM (void) nvlist_remove_nvpair(origprops, match); 331411022STom.Erickson@Sun.COM /* don't bother receiving the property */ 331511022STom.Erickson@Sun.COM (void) nvlist_remove_nvpair(props, pair); 331611022STom.Erickson@Sun.COM next: 331711022STom.Erickson@Sun.COM pair = next_pair; 331811022STom.Erickson@Sun.COM } 331911022STom.Erickson@Sun.COM } 332011022STom.Erickson@Sun.COM 332111022STom.Erickson@Sun.COM #ifdef DEBUG 332211022STom.Erickson@Sun.COM static boolean_t zfs_ioc_recv_inject_err; 332311022STom.Erickson@Sun.COM #endif 332411022STom.Erickson@Sun.COM 33255367Sahrens /* 33265367Sahrens * inputs: 33275367Sahrens * zc_name name of containing filesystem 33285367Sahrens * zc_nvlist_src{_size} nvlist of properties to apply 33295367Sahrens * zc_value name of snapshot to create 33305367Sahrens * zc_string name of clone origin (if DRR_FLAG_CLONE) 33315367Sahrens * zc_cookie file descriptor to recv from 33325367Sahrens * zc_begin_record the BEGIN record of the stream (not byteswapped) 33335367Sahrens * zc_guid force flag 33345367Sahrens * 33355367Sahrens * outputs: 33365367Sahrens * zc_cookie number of bytes read 333711022STom.Erickson@Sun.COM * zc_nvlist_dst{_size} error for each unapplied received property 333811022STom.Erickson@Sun.COM * zc_obj zprop_errflags_t 33395367Sahrens */ 3340789Sahrens static int 33415367Sahrens zfs_ioc_recv(zfs_cmd_t *zc) 3342789Sahrens { 3343789Sahrens file_t *fp; 33445326Sek110237 objset_t *os; 33455367Sahrens dmu_recv_cookie_t drc; 33465326Sek110237 boolean_t force = (boolean_t)zc->zc_guid; 334711022STom.Erickson@Sun.COM int fd; 334811022STom.Erickson@Sun.COM int error = 0; 334911022STom.Erickson@Sun.COM int props_error = 0; 335011022STom.Erickson@Sun.COM nvlist_t *errors; 33515367Sahrens offset_t off; 335211022STom.Erickson@Sun.COM nvlist_t *props = NULL; /* sent properties */ 335311022STom.Erickson@Sun.COM nvlist_t *origprops = NULL; /* existing properties */ 33545367Sahrens objset_t *origin = NULL; 33555367Sahrens char *tosnap; 33565367Sahrens char tofs[ZFS_MAXNAMELEN]; 335711022STom.Erickson@Sun.COM boolean_t first_recvd_props = B_FALSE; 3358789Sahrens 33593265Sahrens if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 || 33605326Sek110237 strchr(zc->zc_value, '@') == NULL || 33615326Sek110237 strchr(zc->zc_value, '%')) 33623265Sahrens return (EINVAL); 33633265Sahrens 33645367Sahrens (void) strcpy(tofs, zc->zc_value); 33655367Sahrens tosnap = strchr(tofs, '@'); 336611022STom.Erickson@Sun.COM *tosnap++ = '\0'; 33675367Sahrens 33685367Sahrens if (zc->zc_nvlist_src != NULL && 33695367Sahrens (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 33709643SEric.Taylor@Sun.COM zc->zc_iflags, &props)) != 0) 33715367Sahrens return (error); 33725367Sahrens 3373789Sahrens fd = zc->zc_cookie; 3374789Sahrens fp = getf(fd); 33755367Sahrens if (fp == NULL) { 33765367Sahrens nvlist_free(props); 3377789Sahrens return (EBADF); 33785367Sahrens } 33795326Sek110237 338011022STom.Erickson@Sun.COM VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0); 338111022STom.Erickson@Sun.COM 338210298SMatthew.Ahrens@Sun.COM if (props && dmu_objset_hold(tofs, FTAG, &os) == 0) { 338311022STom.Erickson@Sun.COM if ((spa_version(os->os_spa) >= SPA_VERSION_RECVD_PROPS) && 338411022STom.Erickson@Sun.COM !dsl_prop_get_hasrecvd(os)) { 338511022STom.Erickson@Sun.COM first_recvd_props = B_TRUE; 338611022STom.Erickson@Sun.COM } 338711022STom.Erickson@Sun.COM 33886689Smaybee /* 338911022STom.Erickson@Sun.COM * If new received properties are supplied, they are to 339011022STom.Erickson@Sun.COM * completely replace the existing received properties, so stash 339111022STom.Erickson@Sun.COM * away the existing ones. 33926689Smaybee */ 339311022STom.Erickson@Sun.COM if (dsl_prop_get_received(os, &origprops) == 0) { 339411022STom.Erickson@Sun.COM nvlist_t *errlist = NULL; 339511022STom.Erickson@Sun.COM /* 339611022STom.Erickson@Sun.COM * Don't bother writing a property if its value won't 339711022STom.Erickson@Sun.COM * change (and avoid the unnecessary security checks). 339811022STom.Erickson@Sun.COM * 339911022STom.Erickson@Sun.COM * The first receive after SPA_VERSION_RECVD_PROPS is a 340011022STom.Erickson@Sun.COM * special case where we blow away all local properties 340111022STom.Erickson@Sun.COM * regardless. 340211022STom.Erickson@Sun.COM */ 340311022STom.Erickson@Sun.COM if (!first_recvd_props) 340411022STom.Erickson@Sun.COM props_reduce(props, origprops); 340511022STom.Erickson@Sun.COM if (zfs_check_clearable(tofs, origprops, 340611022STom.Erickson@Sun.COM &errlist) != 0) 340711022STom.Erickson@Sun.COM (void) nvlist_merge(errors, errlist, 0); 340811022STom.Erickson@Sun.COM nvlist_free(errlist); 340911022STom.Erickson@Sun.COM } 341010298SMatthew.Ahrens@Sun.COM 341110298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 34125326Sek110237 } 34135326Sek110237 34145367Sahrens if (zc->zc_string[0]) { 341510298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(zc->zc_string, FTAG, &origin); 34166689Smaybee if (error) 34176689Smaybee goto out; 34185367Sahrens } 34195367Sahrens 342011007SLori.Alt@Sun.COM error = dmu_recv_begin(tofs, tosnap, zc->zc_top_ds, 342111007SLori.Alt@Sun.COM &zc->zc_begin_record, force, origin, &drc); 34225367Sahrens if (origin) 342310298SMatthew.Ahrens@Sun.COM dmu_objset_rele(origin, FTAG); 34246689Smaybee if (error) 34256689Smaybee goto out; 34265326Sek110237 34275326Sek110237 /* 342811022STom.Erickson@Sun.COM * Set properties before we receive the stream so that they are applied 342911022STom.Erickson@Sun.COM * to the new data. Note that we must call dmu_recv_stream() if 343011022STom.Erickson@Sun.COM * dmu_recv_begin() succeeds. 34315326Sek110237 */ 34325367Sahrens if (props) { 343311022STom.Erickson@Sun.COM nvlist_t *errlist; 343411022STom.Erickson@Sun.COM 343511022STom.Erickson@Sun.COM if (dmu_objset_from_ds(drc.drc_logical_ds, &os) == 0) { 343611022STom.Erickson@Sun.COM if (drc.drc_newfs) { 343711022STom.Erickson@Sun.COM if (spa_version(os->os_spa) >= 343811022STom.Erickson@Sun.COM SPA_VERSION_RECVD_PROPS) 343911022STom.Erickson@Sun.COM first_recvd_props = B_TRUE; 344011022STom.Erickson@Sun.COM } else if (origprops != NULL) { 344111022STom.Erickson@Sun.COM if (clear_received_props(os, tofs, origprops, 344211022STom.Erickson@Sun.COM first_recvd_props ? NULL : props) != 0) 344311022STom.Erickson@Sun.COM zc->zc_obj |= ZPROP_ERR_NOCLEAR; 344411022STom.Erickson@Sun.COM } else { 344511022STom.Erickson@Sun.COM zc->zc_obj |= ZPROP_ERR_NOCLEAR; 344611022STom.Erickson@Sun.COM } 344711022STom.Erickson@Sun.COM dsl_prop_set_hasrecvd(os); 344811022STom.Erickson@Sun.COM } else if (!drc.drc_newfs) { 344911022STom.Erickson@Sun.COM zc->zc_obj |= ZPROP_ERR_NOCLEAR; 345011022STom.Erickson@Sun.COM } 345111022STom.Erickson@Sun.COM 345211022STom.Erickson@Sun.COM (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED, 345311022STom.Erickson@Sun.COM props, &errlist); 345411022STom.Erickson@Sun.COM (void) nvlist_merge(errors, errlist, 0); 345511022STom.Erickson@Sun.COM nvlist_free(errlist); 345611022STom.Erickson@Sun.COM } 345711022STom.Erickson@Sun.COM 345811022STom.Erickson@Sun.COM if (fit_error_list(zc, &errors) != 0 || put_nvlist(zc, errors) != 0) { 34596689Smaybee /* 346011022STom.Erickson@Sun.COM * Caller made zc->zc_nvlist_dst less than the minimum expected 346111022STom.Erickson@Sun.COM * size or supplied an invalid address. 34626689Smaybee */ 346311022STom.Erickson@Sun.COM props_error = EINVAL; 34645367Sahrens } 34655367Sahrens 34665367Sahrens off = fp->f_offset; 34675367Sahrens error = dmu_recv_stream(&drc, fp->f_vnode, &off); 34685367Sahrens 346910204SMatthew.Ahrens@Sun.COM if (error == 0) { 347010204SMatthew.Ahrens@Sun.COM zfsvfs_t *zfsvfs = NULL; 347110204SMatthew.Ahrens@Sun.COM 347210204SMatthew.Ahrens@Sun.COM if (getzfsvfs(tofs, &zfsvfs) == 0) { 347310204SMatthew.Ahrens@Sun.COM /* online recv */ 347410204SMatthew.Ahrens@Sun.COM int end_err; 347510298SMatthew.Ahrens@Sun.COM 347610298SMatthew.Ahrens@Sun.COM error = zfs_suspend_fs(zfsvfs); 347710204SMatthew.Ahrens@Sun.COM /* 347810204SMatthew.Ahrens@Sun.COM * If the suspend fails, then the recv_end will 347910204SMatthew.Ahrens@Sun.COM * likely also fail, and clean up after itself. 348010204SMatthew.Ahrens@Sun.COM */ 348110204SMatthew.Ahrens@Sun.COM end_err = dmu_recv_end(&drc); 348211812SGeorge.Wilson@Sun.COM if (error == 0) 348311812SGeorge.Wilson@Sun.COM error = zfs_resume_fs(zfsvfs, tofs); 348410204SMatthew.Ahrens@Sun.COM error = error ? error : end_err; 348510204SMatthew.Ahrens@Sun.COM VFS_RELE(zfsvfs->z_vfs); 348610204SMatthew.Ahrens@Sun.COM } else { 34876689Smaybee error = dmu_recv_end(&drc); 34885367Sahrens } 34896083Sek110237 } 34905367Sahrens 34915367Sahrens zc->zc_cookie = off - fp->f_offset; 34925367Sahrens if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0) 34935367Sahrens fp->f_offset = off; 34942885Sahrens 349511022STom.Erickson@Sun.COM #ifdef DEBUG 349611022STom.Erickson@Sun.COM if (zfs_ioc_recv_inject_err) { 349711022STom.Erickson@Sun.COM zfs_ioc_recv_inject_err = B_FALSE; 349811022STom.Erickson@Sun.COM error = 1; 349911022STom.Erickson@Sun.COM } 350011022STom.Erickson@Sun.COM #endif 35016689Smaybee /* 35026689Smaybee * On error, restore the original props. 35036689Smaybee */ 35046689Smaybee if (error && props) { 350511022STom.Erickson@Sun.COM if (dmu_objset_hold(tofs, FTAG, &os) == 0) { 350611022STom.Erickson@Sun.COM if (clear_received_props(os, tofs, props, NULL) != 0) { 350711022STom.Erickson@Sun.COM /* 350811022STom.Erickson@Sun.COM * We failed to clear the received properties. 350911022STom.Erickson@Sun.COM * Since we may have left a $recvd value on the 351011022STom.Erickson@Sun.COM * system, we can't clear the $hasrecvd flag. 351111022STom.Erickson@Sun.COM */ 351211022STom.Erickson@Sun.COM zc->zc_obj |= ZPROP_ERR_NORESTORE; 351311022STom.Erickson@Sun.COM } else if (first_recvd_props) { 351411022STom.Erickson@Sun.COM dsl_prop_unset_hasrecvd(os); 351511022STom.Erickson@Sun.COM } 351611022STom.Erickson@Sun.COM dmu_objset_rele(os, FTAG); 351711022STom.Erickson@Sun.COM } else if (!drc.drc_newfs) { 351811022STom.Erickson@Sun.COM /* We failed to clear the received properties. */ 351911022STom.Erickson@Sun.COM zc->zc_obj |= ZPROP_ERR_NORESTORE; 352011022STom.Erickson@Sun.COM } 352111022STom.Erickson@Sun.COM 352211022STom.Erickson@Sun.COM if (origprops == NULL && !drc.drc_newfs) { 352311022STom.Erickson@Sun.COM /* We failed to stash the original properties. */ 352411022STom.Erickson@Sun.COM zc->zc_obj |= ZPROP_ERR_NORESTORE; 352511022STom.Erickson@Sun.COM } 352611022STom.Erickson@Sun.COM 352711022STom.Erickson@Sun.COM /* 352811022STom.Erickson@Sun.COM * dsl_props_set() will not convert RECEIVED to LOCAL on or 352911022STom.Erickson@Sun.COM * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL 353011022STom.Erickson@Sun.COM * explictly if we're restoring local properties cleared in the 353111022STom.Erickson@Sun.COM * first new-style receive. 353211022STom.Erickson@Sun.COM */ 353311022STom.Erickson@Sun.COM if (origprops != NULL && 353411022STom.Erickson@Sun.COM zfs_set_prop_nvlist(tofs, (first_recvd_props ? 353511022STom.Erickson@Sun.COM ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED), 353611022STom.Erickson@Sun.COM origprops, NULL) != 0) { 353711022STom.Erickson@Sun.COM /* 353811022STom.Erickson@Sun.COM * We stashed the original properties but failed to 353911022STom.Erickson@Sun.COM * restore them. 354011022STom.Erickson@Sun.COM */ 354111022STom.Erickson@Sun.COM zc->zc_obj |= ZPROP_ERR_NORESTORE; 354211022STom.Erickson@Sun.COM } 35436689Smaybee } 35446689Smaybee out: 35456689Smaybee nvlist_free(props); 35466689Smaybee nvlist_free(origprops); 354711022STom.Erickson@Sun.COM nvlist_free(errors); 3548789Sahrens releasef(fd); 354911022STom.Erickson@Sun.COM 355011022STom.Erickson@Sun.COM if (error == 0) 355111022STom.Erickson@Sun.COM error = props_error; 355211022STom.Erickson@Sun.COM 3553789Sahrens return (error); 3554789Sahrens } 3555789Sahrens 35565367Sahrens /* 35575367Sahrens * inputs: 35585367Sahrens * zc_name name of snapshot to send 35595367Sahrens * zc_value short name of incremental fromsnap (may be empty) 35605367Sahrens * zc_cookie file descriptor to send stream to 35615367Sahrens * zc_obj fromorigin flag (mutually exclusive with zc_value) 35625367Sahrens * 35635367Sahrens * outputs: none 35645367Sahrens */ 3565789Sahrens static int 35665367Sahrens zfs_ioc_send(zfs_cmd_t *zc) 3567789Sahrens { 3568789Sahrens objset_t *fromsnap = NULL; 3569789Sahrens objset_t *tosnap; 3570789Sahrens file_t *fp; 3571789Sahrens int error; 35725367Sahrens offset_t off; 3573789Sahrens 357410298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(zc->zc_name, FTAG, &tosnap); 3575789Sahrens if (error) 3576789Sahrens return (error); 3577789Sahrens 35782676Seschrock if (zc->zc_value[0] != '\0') { 35798012SEric.Taylor@Sun.COM char *buf; 35802885Sahrens char *cp; 35812885Sahrens 35828012SEric.Taylor@Sun.COM buf = kmem_alloc(MAXPATHLEN, KM_SLEEP); 35838012SEric.Taylor@Sun.COM (void) strncpy(buf, zc->zc_name, MAXPATHLEN); 35842885Sahrens cp = strchr(buf, '@'); 35852885Sahrens if (cp) 35862885Sahrens *(cp+1) = 0; 35878012SEric.Taylor@Sun.COM (void) strncat(buf, zc->zc_value, MAXPATHLEN); 358810298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(buf, FTAG, &fromsnap); 35898012SEric.Taylor@Sun.COM kmem_free(buf, MAXPATHLEN); 3590789Sahrens if (error) { 359110298SMatthew.Ahrens@Sun.COM dmu_objset_rele(tosnap, FTAG); 3592789Sahrens return (error); 3593789Sahrens } 3594789Sahrens } 3595789Sahrens 3596789Sahrens fp = getf(zc->zc_cookie); 3597789Sahrens if (fp == NULL) { 359810298SMatthew.Ahrens@Sun.COM dmu_objset_rele(tosnap, FTAG); 3599789Sahrens if (fromsnap) 360010298SMatthew.Ahrens@Sun.COM dmu_objset_rele(fromsnap, FTAG); 3601789Sahrens return (EBADF); 3602789Sahrens } 3603789Sahrens 36045367Sahrens off = fp->f_offset; 36055367Sahrens error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp->f_vnode, &off); 36065367Sahrens 36075367Sahrens if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0) 36085367Sahrens fp->f_offset = off; 3609789Sahrens releasef(zc->zc_cookie); 3610789Sahrens if (fromsnap) 361110298SMatthew.Ahrens@Sun.COM dmu_objset_rele(fromsnap, FTAG); 361210298SMatthew.Ahrens@Sun.COM dmu_objset_rele(tosnap, FTAG); 3613789Sahrens return (error); 3614789Sahrens } 3615789Sahrens 36161544Seschrock static int 36171544Seschrock zfs_ioc_inject_fault(zfs_cmd_t *zc) 36181544Seschrock { 36191544Seschrock int id, error; 36201544Seschrock 36211544Seschrock error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id, 36221544Seschrock &zc->zc_inject_record); 36231544Seschrock 36241544Seschrock if (error == 0) 36251544Seschrock zc->zc_guid = (uint64_t)id; 36261544Seschrock 36271544Seschrock return (error); 36281544Seschrock } 36291544Seschrock 36301544Seschrock static int 36311544Seschrock zfs_ioc_clear_fault(zfs_cmd_t *zc) 36321544Seschrock { 36331544Seschrock return (zio_clear_fault((int)zc->zc_guid)); 36341544Seschrock } 36351544Seschrock 36361544Seschrock static int 36371544Seschrock zfs_ioc_inject_list_next(zfs_cmd_t *zc) 36381544Seschrock { 36391544Seschrock int id = (int)zc->zc_guid; 36401544Seschrock int error; 36411544Seschrock 36421544Seschrock error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name), 36431544Seschrock &zc->zc_inject_record); 36441544Seschrock 36451544Seschrock zc->zc_guid = id; 36461544Seschrock 36471544Seschrock return (error); 36481544Seschrock } 36491544Seschrock 36501544Seschrock static int 36511544Seschrock zfs_ioc_error_log(zfs_cmd_t *zc) 36521544Seschrock { 36531544Seschrock spa_t *spa; 36541544Seschrock int error; 36552676Seschrock size_t count = (size_t)zc->zc_nvlist_dst_size; 36561544Seschrock 36571544Seschrock if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 36581544Seschrock return (error); 36591544Seschrock 36602676Seschrock error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst, 36611544Seschrock &count); 36621544Seschrock if (error == 0) 36632676Seschrock zc->zc_nvlist_dst_size = count; 36641544Seschrock else 36652676Seschrock zc->zc_nvlist_dst_size = spa_get_errlog_size(spa); 36661544Seschrock 36671544Seschrock spa_close(spa, FTAG); 36681544Seschrock 36691544Seschrock return (error); 36701544Seschrock } 36711544Seschrock 36721544Seschrock static int 36731544Seschrock zfs_ioc_clear(zfs_cmd_t *zc) 36741544Seschrock { 36751544Seschrock spa_t *spa; 36761544Seschrock vdev_t *vd; 36771544Seschrock int error; 36781544Seschrock 36797294Sperrin /* 36807294Sperrin * On zpool clear we also fix up missing slogs 36817294Sperrin */ 36827294Sperrin mutex_enter(&spa_namespace_lock); 36837294Sperrin spa = spa_lookup(zc->zc_name); 36847294Sperrin if (spa == NULL) { 36857294Sperrin mutex_exit(&spa_namespace_lock); 36867294Sperrin return (EIO); 36877294Sperrin } 368810922SJeff.Bonwick@Sun.COM if (spa_get_log_state(spa) == SPA_LOG_MISSING) { 36897294Sperrin /* we need to let spa_open/spa_load clear the chains */ 369010922SJeff.Bonwick@Sun.COM spa_set_log_state(spa, SPA_LOG_CLEAR); 36917294Sperrin } 369210921STim.Haley@Sun.COM spa->spa_last_open_failed = 0; 36937294Sperrin mutex_exit(&spa_namespace_lock); 36947294Sperrin 369511727SVictor.Latushkin@Sun.COM if (zc->zc_cookie & ZPOOL_NO_REWIND) { 369610921STim.Haley@Sun.COM error = spa_open(zc->zc_name, &spa, FTAG); 369710921STim.Haley@Sun.COM } else { 369810921STim.Haley@Sun.COM nvlist_t *policy; 369910921STim.Haley@Sun.COM nvlist_t *config = NULL; 370010921STim.Haley@Sun.COM 370110921STim.Haley@Sun.COM if (zc->zc_nvlist_src == NULL) 370210921STim.Haley@Sun.COM return (EINVAL); 370310921STim.Haley@Sun.COM 370410921STim.Haley@Sun.COM if ((error = get_nvlist(zc->zc_nvlist_src, 370510921STim.Haley@Sun.COM zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) { 370610921STim.Haley@Sun.COM error = spa_open_rewind(zc->zc_name, &spa, FTAG, 370710921STim.Haley@Sun.COM policy, &config); 370810921STim.Haley@Sun.COM if (config != NULL) { 370910921STim.Haley@Sun.COM (void) put_nvlist(zc, config); 371010921STim.Haley@Sun.COM nvlist_free(config); 371110921STim.Haley@Sun.COM } 371210921STim.Haley@Sun.COM nvlist_free(policy); 371310921STim.Haley@Sun.COM } 371410921STim.Haley@Sun.COM } 371510921STim.Haley@Sun.COM 371610921STim.Haley@Sun.COM if (error) 37171544Seschrock return (error); 37181544Seschrock 371910685SGeorge.Wilson@Sun.COM spa_vdev_state_enter(spa, SCL_NONE); 37201544Seschrock 37212676Seschrock if (zc->zc_guid == 0) { 37221544Seschrock vd = NULL; 37236643Seschrock } else { 37246643Seschrock vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE); 37255450Sbrendan if (vd == NULL) { 37267754SJeff.Bonwick@Sun.COM (void) spa_vdev_state_exit(spa, NULL, ENODEV); 37275450Sbrendan spa_close(spa, FTAG); 37285450Sbrendan return (ENODEV); 37295450Sbrendan } 37301544Seschrock } 37311544Seschrock 37327754SJeff.Bonwick@Sun.COM vdev_clear(spa, vd); 37337754SJeff.Bonwick@Sun.COM 37347754SJeff.Bonwick@Sun.COM (void) spa_vdev_state_exit(spa, NULL, 0); 37357754SJeff.Bonwick@Sun.COM 37367754SJeff.Bonwick@Sun.COM /* 37377754SJeff.Bonwick@Sun.COM * Resume any suspended I/Os. 37387754SJeff.Bonwick@Sun.COM */ 37399234SGeorge.Wilson@Sun.COM if (zio_resume(spa) != 0) 37409234SGeorge.Wilson@Sun.COM error = EIO; 37411544Seschrock 37421544Seschrock spa_close(spa, FTAG); 37431544Seschrock 37449234SGeorge.Wilson@Sun.COM return (error); 37451544Seschrock } 37461544Seschrock 37475367Sahrens /* 37485367Sahrens * inputs: 37495367Sahrens * zc_name name of filesystem 37505367Sahrens * zc_value name of origin snapshot 37515367Sahrens * 375210588SEric.Taylor@Sun.COM * outputs: 375310588SEric.Taylor@Sun.COM * zc_string name of conflicting snapshot, if there is one 37545367Sahrens */ 37551544Seschrock static int 37562082Seschrock zfs_ioc_promote(zfs_cmd_t *zc) 37572082Seschrock { 37582417Sahrens char *cp; 37592417Sahrens 37602417Sahrens /* 37612417Sahrens * We don't need to unmount *all* the origin fs's snapshots, but 37622417Sahrens * it's easier. 37632417Sahrens */ 37642676Seschrock cp = strchr(zc->zc_value, '@'); 37652417Sahrens if (cp) 37662417Sahrens *cp = '\0'; 37672676Seschrock (void) dmu_objset_find(zc->zc_value, 37682417Sahrens zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS); 376910588SEric.Taylor@Sun.COM return (dsl_dataset_promote(zc->zc_name, zc->zc_string)); 37702082Seschrock } 37712082Seschrock 37724543Smarks /* 37739396SMatthew.Ahrens@Sun.COM * Retrieve a single {user|group}{used|quota}@... property. 37749396SMatthew.Ahrens@Sun.COM * 37759396SMatthew.Ahrens@Sun.COM * inputs: 37769396SMatthew.Ahrens@Sun.COM * zc_name name of filesystem 37779396SMatthew.Ahrens@Sun.COM * zc_objset_type zfs_userquota_prop_t 37789396SMatthew.Ahrens@Sun.COM * zc_value domain name (eg. "S-1-234-567-89") 37799396SMatthew.Ahrens@Sun.COM * zc_guid RID/UID/GID 37809396SMatthew.Ahrens@Sun.COM * 37819396SMatthew.Ahrens@Sun.COM * outputs: 37829396SMatthew.Ahrens@Sun.COM * zc_cookie property value 37839396SMatthew.Ahrens@Sun.COM */ 37849396SMatthew.Ahrens@Sun.COM static int 37859396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_one(zfs_cmd_t *zc) 37869396SMatthew.Ahrens@Sun.COM { 37879396SMatthew.Ahrens@Sun.COM zfsvfs_t *zfsvfs; 37889396SMatthew.Ahrens@Sun.COM int error; 37899396SMatthew.Ahrens@Sun.COM 37909396SMatthew.Ahrens@Sun.COM if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS) 37919396SMatthew.Ahrens@Sun.COM return (EINVAL); 37929396SMatthew.Ahrens@Sun.COM 379310298SMatthew.Ahrens@Sun.COM error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs); 37949396SMatthew.Ahrens@Sun.COM if (error) 37959396SMatthew.Ahrens@Sun.COM return (error); 37969396SMatthew.Ahrens@Sun.COM 37979396SMatthew.Ahrens@Sun.COM error = zfs_userspace_one(zfsvfs, 37989396SMatthew.Ahrens@Sun.COM zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie); 37999396SMatthew.Ahrens@Sun.COM zfsvfs_rele(zfsvfs, FTAG); 38009396SMatthew.Ahrens@Sun.COM 38019396SMatthew.Ahrens@Sun.COM return (error); 38029396SMatthew.Ahrens@Sun.COM } 38039396SMatthew.Ahrens@Sun.COM 38049396SMatthew.Ahrens@Sun.COM /* 38059396SMatthew.Ahrens@Sun.COM * inputs: 38069396SMatthew.Ahrens@Sun.COM * zc_name name of filesystem 38079396SMatthew.Ahrens@Sun.COM * zc_cookie zap cursor 38089396SMatthew.Ahrens@Sun.COM * zc_objset_type zfs_userquota_prop_t 38099396SMatthew.Ahrens@Sun.COM * zc_nvlist_dst[_size] buffer to fill (not really an nvlist) 38109396SMatthew.Ahrens@Sun.COM * 38119396SMatthew.Ahrens@Sun.COM * outputs: 38129396SMatthew.Ahrens@Sun.COM * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t) 38139396SMatthew.Ahrens@Sun.COM * zc_cookie zap cursor 38149396SMatthew.Ahrens@Sun.COM */ 38159396SMatthew.Ahrens@Sun.COM static int 38169396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_many(zfs_cmd_t *zc) 38179396SMatthew.Ahrens@Sun.COM { 38189396SMatthew.Ahrens@Sun.COM zfsvfs_t *zfsvfs; 3819*11933STim.Haley@Sun.COM int bufsize = zc->zc_nvlist_dst_size; 3820*11933STim.Haley@Sun.COM 3821*11933STim.Haley@Sun.COM if (bufsize <= 0) 3822*11933STim.Haley@Sun.COM return (ENOMEM); 3823*11933STim.Haley@Sun.COM 3824*11933STim.Haley@Sun.COM int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs); 38259396SMatthew.Ahrens@Sun.COM if (error) 38269396SMatthew.Ahrens@Sun.COM return (error); 38279396SMatthew.Ahrens@Sun.COM 38289396SMatthew.Ahrens@Sun.COM void *buf = kmem_alloc(bufsize, KM_SLEEP); 38299396SMatthew.Ahrens@Sun.COM 38309396SMatthew.Ahrens@Sun.COM error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie, 38319396SMatthew.Ahrens@Sun.COM buf, &zc->zc_nvlist_dst_size); 38329396SMatthew.Ahrens@Sun.COM 38339396SMatthew.Ahrens@Sun.COM if (error == 0) { 38349396SMatthew.Ahrens@Sun.COM error = xcopyout(buf, 38359396SMatthew.Ahrens@Sun.COM (void *)(uintptr_t)zc->zc_nvlist_dst, 38369396SMatthew.Ahrens@Sun.COM zc->zc_nvlist_dst_size); 38379396SMatthew.Ahrens@Sun.COM } 38389396SMatthew.Ahrens@Sun.COM kmem_free(buf, bufsize); 38399396SMatthew.Ahrens@Sun.COM zfsvfs_rele(zfsvfs, FTAG); 38409396SMatthew.Ahrens@Sun.COM 38419396SMatthew.Ahrens@Sun.COM return (error); 38429396SMatthew.Ahrens@Sun.COM } 38439396SMatthew.Ahrens@Sun.COM 38449396SMatthew.Ahrens@Sun.COM /* 38459396SMatthew.Ahrens@Sun.COM * inputs: 38469396SMatthew.Ahrens@Sun.COM * zc_name name of filesystem 38479396SMatthew.Ahrens@Sun.COM * 38489396SMatthew.Ahrens@Sun.COM * outputs: 38499396SMatthew.Ahrens@Sun.COM * none 38509396SMatthew.Ahrens@Sun.COM */ 38519396SMatthew.Ahrens@Sun.COM static int 38529396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_upgrade(zfs_cmd_t *zc) 38539396SMatthew.Ahrens@Sun.COM { 38549396SMatthew.Ahrens@Sun.COM objset_t *os; 385511422SMark.Musante@Sun.COM int error = 0; 38569396SMatthew.Ahrens@Sun.COM zfsvfs_t *zfsvfs; 38579396SMatthew.Ahrens@Sun.COM 38589396SMatthew.Ahrens@Sun.COM if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) { 385910298SMatthew.Ahrens@Sun.COM if (!dmu_objset_userused_enabled(zfsvfs->z_os)) { 38609396SMatthew.Ahrens@Sun.COM /* 38619396SMatthew.Ahrens@Sun.COM * If userused is not enabled, it may be because the 38629396SMatthew.Ahrens@Sun.COM * objset needs to be closed & reopened (to grow the 38639396SMatthew.Ahrens@Sun.COM * objset_phys_t). Suspend/resume the fs will do that. 38649396SMatthew.Ahrens@Sun.COM */ 386510298SMatthew.Ahrens@Sun.COM error = zfs_suspend_fs(zfsvfs); 386610298SMatthew.Ahrens@Sun.COM if (error == 0) 386710298SMatthew.Ahrens@Sun.COM error = zfs_resume_fs(zfsvfs, zc->zc_name); 38689396SMatthew.Ahrens@Sun.COM } 38699396SMatthew.Ahrens@Sun.COM if (error == 0) 38709396SMatthew.Ahrens@Sun.COM error = dmu_objset_userspace_upgrade(zfsvfs->z_os); 38719396SMatthew.Ahrens@Sun.COM VFS_RELE(zfsvfs->z_vfs); 38729396SMatthew.Ahrens@Sun.COM } else { 387310298SMatthew.Ahrens@Sun.COM /* XXX kind of reading contents without owning */ 387410298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(zc->zc_name, FTAG, &os); 38759396SMatthew.Ahrens@Sun.COM if (error) 38769396SMatthew.Ahrens@Sun.COM return (error); 38779396SMatthew.Ahrens@Sun.COM 38789396SMatthew.Ahrens@Sun.COM error = dmu_objset_userspace_upgrade(os); 387910298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 38809396SMatthew.Ahrens@Sun.COM } 38819396SMatthew.Ahrens@Sun.COM 38829396SMatthew.Ahrens@Sun.COM return (error); 38839396SMatthew.Ahrens@Sun.COM } 38849396SMatthew.Ahrens@Sun.COM 38859396SMatthew.Ahrens@Sun.COM /* 38864543Smarks * We don't want to have a hard dependency 38874543Smarks * against some special symbols in sharefs 38885331Samw * nfs, and smbsrv. Determine them if needed when 38894543Smarks * the first file system is shared. 38905331Samw * Neither sharefs, nfs or smbsrv are unloadable modules. 38914543Smarks */ 38925331Samw int (*znfsexport_fs)(void *arg); 38934543Smarks int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t); 38945331Samw int (*zsmbexport_fs)(void *arg, boolean_t add_share); 38955331Samw 38965331Samw int zfs_nfsshare_inited; 38975331Samw int zfs_smbshare_inited; 38985331Samw 38994543Smarks ddi_modhandle_t nfs_mod; 39004543Smarks ddi_modhandle_t sharefs_mod; 39015331Samw ddi_modhandle_t smbsrv_mod; 39024543Smarks kmutex_t zfs_share_lock; 39034543Smarks 39044543Smarks static int 39055331Samw zfs_init_sharefs() 39065331Samw { 39075331Samw int error; 39085331Samw 39095331Samw ASSERT(MUTEX_HELD(&zfs_share_lock)); 39105331Samw /* Both NFS and SMB shares also require sharetab support. */ 39115331Samw if (sharefs_mod == NULL && ((sharefs_mod = 39125331Samw ddi_modopen("fs/sharefs", 39135331Samw KRTLD_MODE_FIRST, &error)) == NULL)) { 39145331Samw return (ENOSYS); 39155331Samw } 39165331Samw if (zshare_fs == NULL && ((zshare_fs = 39175331Samw (int (*)(enum sharefs_sys_op, share_t *, uint32_t)) 39185331Samw ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) { 39195331Samw return (ENOSYS); 39205331Samw } 39215331Samw return (0); 39225331Samw } 39235331Samw 39245331Samw static int 39254543Smarks zfs_ioc_share(zfs_cmd_t *zc) 39264543Smarks { 39274543Smarks int error; 39284543Smarks int opcode; 39294543Smarks 39305331Samw switch (zc->zc_share.z_sharetype) { 39315331Samw case ZFS_SHARE_NFS: 39325331Samw case ZFS_UNSHARE_NFS: 39335331Samw if (zfs_nfsshare_inited == 0) { 39345331Samw mutex_enter(&zfs_share_lock); 39355331Samw if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs", 39365331Samw KRTLD_MODE_FIRST, &error)) == NULL)) { 39375331Samw mutex_exit(&zfs_share_lock); 39385331Samw return (ENOSYS); 39395331Samw } 39405331Samw if (znfsexport_fs == NULL && 39415331Samw ((znfsexport_fs = (int (*)(void *)) 39425331Samw ddi_modsym(nfs_mod, 39435331Samw "nfs_export", &error)) == NULL)) { 39445331Samw mutex_exit(&zfs_share_lock); 39455331Samw return (ENOSYS); 39465331Samw } 39475331Samw error = zfs_init_sharefs(); 39485331Samw if (error) { 39495331Samw mutex_exit(&zfs_share_lock); 39505331Samw return (ENOSYS); 39515331Samw } 39525331Samw zfs_nfsshare_inited = 1; 39534543Smarks mutex_exit(&zfs_share_lock); 39544543Smarks } 39555331Samw break; 39565331Samw case ZFS_SHARE_SMB: 39575331Samw case ZFS_UNSHARE_SMB: 39585331Samw if (zfs_smbshare_inited == 0) { 39595331Samw mutex_enter(&zfs_share_lock); 39605331Samw if (smbsrv_mod == NULL && ((smbsrv_mod = 39615331Samw ddi_modopen("drv/smbsrv", 39625331Samw KRTLD_MODE_FIRST, &error)) == NULL)) { 39635331Samw mutex_exit(&zfs_share_lock); 39645331Samw return (ENOSYS); 39655331Samw } 39665331Samw if (zsmbexport_fs == NULL && ((zsmbexport_fs = 39675331Samw (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod, 39686139Sjb150015 "smb_server_share", &error)) == NULL)) { 39695331Samw mutex_exit(&zfs_share_lock); 39705331Samw return (ENOSYS); 39715331Samw } 39725331Samw error = zfs_init_sharefs(); 39735331Samw if (error) { 39745331Samw mutex_exit(&zfs_share_lock); 39755331Samw return (ENOSYS); 39765331Samw } 39775331Samw zfs_smbshare_inited = 1; 39784543Smarks mutex_exit(&zfs_share_lock); 39794543Smarks } 39805331Samw break; 39815331Samw default: 39825331Samw return (EINVAL); 39834543Smarks } 39844543Smarks 39855331Samw switch (zc->zc_share.z_sharetype) { 39865331Samw case ZFS_SHARE_NFS: 39875331Samw case ZFS_UNSHARE_NFS: 39885331Samw if (error = 39895331Samw znfsexport_fs((void *) 39905331Samw (uintptr_t)zc->zc_share.z_exportdata)) 39915331Samw return (error); 39925331Samw break; 39935331Samw case ZFS_SHARE_SMB: 39945331Samw case ZFS_UNSHARE_SMB: 39955331Samw if (error = zsmbexport_fs((void *) 39965331Samw (uintptr_t)zc->zc_share.z_exportdata, 39975331Samw zc->zc_share.z_sharetype == ZFS_SHARE_SMB ? 39988845Samw@Sun.COM B_TRUE: B_FALSE)) { 39995331Samw return (error); 40005331Samw } 40015331Samw break; 40025331Samw } 40035331Samw 40045331Samw opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS || 40055331Samw zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ? 40064543Smarks SHAREFS_ADD : SHAREFS_REMOVE; 40074543Smarks 40085331Samw /* 40095331Samw * Add or remove share from sharetab 40105331Samw */ 40114543Smarks error = zshare_fs(opcode, 40124543Smarks (void *)(uintptr_t)zc->zc_share.z_sharedata, 40134543Smarks zc->zc_share.z_sharemax); 40144543Smarks 40154543Smarks return (error); 40164543Smarks 40174543Smarks } 40184543Smarks 40198845Samw@Sun.COM ace_t full_access[] = { 40208845Samw@Sun.COM {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0} 40218845Samw@Sun.COM }; 40228845Samw@Sun.COM 40238845Samw@Sun.COM /* 40248845Samw@Sun.COM * Remove all ACL files in shares dir 40258845Samw@Sun.COM */ 40268845Samw@Sun.COM static int 40278845Samw@Sun.COM zfs_smb_acl_purge(znode_t *dzp) 40288845Samw@Sun.COM { 40298845Samw@Sun.COM zap_cursor_t zc; 40308845Samw@Sun.COM zap_attribute_t zap; 40318845Samw@Sun.COM zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 40328845Samw@Sun.COM int error; 40338845Samw@Sun.COM 40348845Samw@Sun.COM for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id); 40358845Samw@Sun.COM (error = zap_cursor_retrieve(&zc, &zap)) == 0; 40368845Samw@Sun.COM zap_cursor_advance(&zc)) { 40378845Samw@Sun.COM if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred, 40388845Samw@Sun.COM NULL, 0)) != 0) 40398845Samw@Sun.COM break; 40408845Samw@Sun.COM } 40418845Samw@Sun.COM zap_cursor_fini(&zc); 40428845Samw@Sun.COM return (error); 40438845Samw@Sun.COM } 40448845Samw@Sun.COM 40458845Samw@Sun.COM static int 40468845Samw@Sun.COM zfs_ioc_smb_acl(zfs_cmd_t *zc) 40478845Samw@Sun.COM { 40488845Samw@Sun.COM vnode_t *vp; 40498845Samw@Sun.COM znode_t *dzp; 40508845Samw@Sun.COM vnode_t *resourcevp = NULL; 40518845Samw@Sun.COM znode_t *sharedir; 40528845Samw@Sun.COM zfsvfs_t *zfsvfs; 40538845Samw@Sun.COM nvlist_t *nvlist; 40548845Samw@Sun.COM char *src, *target; 40558845Samw@Sun.COM vattr_t vattr; 40568845Samw@Sun.COM vsecattr_t vsec; 40578845Samw@Sun.COM int error = 0; 40588845Samw@Sun.COM 40598845Samw@Sun.COM if ((error = lookupname(zc->zc_value, UIO_SYSSPACE, 40608845Samw@Sun.COM NO_FOLLOW, NULL, &vp)) != 0) 40618845Samw@Sun.COM return (error); 40628845Samw@Sun.COM 40638845Samw@Sun.COM /* Now make sure mntpnt and dataset are ZFS */ 40648845Samw@Sun.COM 40658845Samw@Sun.COM if (vp->v_vfsp->vfs_fstype != zfsfstype || 40668845Samw@Sun.COM (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource), 40678845Samw@Sun.COM zc->zc_name) != 0)) { 40688845Samw@Sun.COM VN_RELE(vp); 40698845Samw@Sun.COM return (EINVAL); 40708845Samw@Sun.COM } 40718845Samw@Sun.COM 40728845Samw@Sun.COM dzp = VTOZ(vp); 40738845Samw@Sun.COM zfsvfs = dzp->z_zfsvfs; 40748845Samw@Sun.COM ZFS_ENTER(zfsvfs); 40758845Samw@Sun.COM 40769030SMark.Shellenbaum@Sun.COM /* 40779030SMark.Shellenbaum@Sun.COM * Create share dir if its missing. 40789030SMark.Shellenbaum@Sun.COM */ 40799030SMark.Shellenbaum@Sun.COM mutex_enter(&zfsvfs->z_lock); 40809030SMark.Shellenbaum@Sun.COM if (zfsvfs->z_shares_dir == 0) { 40819030SMark.Shellenbaum@Sun.COM dmu_tx_t *tx; 40829030SMark.Shellenbaum@Sun.COM 40839030SMark.Shellenbaum@Sun.COM tx = dmu_tx_create(zfsvfs->z_os); 40849030SMark.Shellenbaum@Sun.COM dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE, 40859030SMark.Shellenbaum@Sun.COM ZFS_SHARES_DIR); 40869030SMark.Shellenbaum@Sun.COM dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL); 40879030SMark.Shellenbaum@Sun.COM error = dmu_tx_assign(tx, TXG_WAIT); 40889030SMark.Shellenbaum@Sun.COM if (error) { 40899030SMark.Shellenbaum@Sun.COM dmu_tx_abort(tx); 40909030SMark.Shellenbaum@Sun.COM } else { 40919030SMark.Shellenbaum@Sun.COM error = zfs_create_share_dir(zfsvfs, tx); 40929030SMark.Shellenbaum@Sun.COM dmu_tx_commit(tx); 40939030SMark.Shellenbaum@Sun.COM } 40949030SMark.Shellenbaum@Sun.COM if (error) { 40959030SMark.Shellenbaum@Sun.COM mutex_exit(&zfsvfs->z_lock); 40969030SMark.Shellenbaum@Sun.COM VN_RELE(vp); 40979030SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 40989030SMark.Shellenbaum@Sun.COM return (error); 40999030SMark.Shellenbaum@Sun.COM } 41009030SMark.Shellenbaum@Sun.COM } 41019030SMark.Shellenbaum@Sun.COM mutex_exit(&zfsvfs->z_lock); 41029030SMark.Shellenbaum@Sun.COM 41039030SMark.Shellenbaum@Sun.COM ASSERT(zfsvfs->z_shares_dir); 41048845Samw@Sun.COM if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) { 41059030SMark.Shellenbaum@Sun.COM VN_RELE(vp); 41068845Samw@Sun.COM ZFS_EXIT(zfsvfs); 41078845Samw@Sun.COM return (error); 41088845Samw@Sun.COM } 41098845Samw@Sun.COM 41108845Samw@Sun.COM switch (zc->zc_cookie) { 41118845Samw@Sun.COM case ZFS_SMB_ACL_ADD: 41128845Samw@Sun.COM vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE; 41138845Samw@Sun.COM vattr.va_type = VREG; 41148845Samw@Sun.COM vattr.va_mode = S_IFREG|0777; 41158845Samw@Sun.COM vattr.va_uid = 0; 41168845Samw@Sun.COM vattr.va_gid = 0; 41178845Samw@Sun.COM 41188845Samw@Sun.COM vsec.vsa_mask = VSA_ACE; 41198845Samw@Sun.COM vsec.vsa_aclentp = &full_access; 41208845Samw@Sun.COM vsec.vsa_aclentsz = sizeof (full_access); 41218845Samw@Sun.COM vsec.vsa_aclcnt = 1; 41228845Samw@Sun.COM 41238845Samw@Sun.COM error = VOP_CREATE(ZTOV(sharedir), zc->zc_string, 41248845Samw@Sun.COM &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec); 41258845Samw@Sun.COM if (resourcevp) 41268845Samw@Sun.COM VN_RELE(resourcevp); 41278845Samw@Sun.COM break; 41288845Samw@Sun.COM 41298845Samw@Sun.COM case ZFS_SMB_ACL_REMOVE: 41308845Samw@Sun.COM error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred, 41318845Samw@Sun.COM NULL, 0); 41328845Samw@Sun.COM break; 41338845Samw@Sun.COM 41348845Samw@Sun.COM case ZFS_SMB_ACL_RENAME: 41358845Samw@Sun.COM if ((error = get_nvlist(zc->zc_nvlist_src, 41369643SEric.Taylor@Sun.COM zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) { 41378845Samw@Sun.COM VN_RELE(vp); 41388845Samw@Sun.COM ZFS_EXIT(zfsvfs); 41398845Samw@Sun.COM return (error); 41408845Samw@Sun.COM } 41418845Samw@Sun.COM if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) || 41428845Samw@Sun.COM nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET, 41438845Samw@Sun.COM &target)) { 41448845Samw@Sun.COM VN_RELE(vp); 41459179SMark.Shellenbaum@Sun.COM VN_RELE(ZTOV(sharedir)); 41468845Samw@Sun.COM ZFS_EXIT(zfsvfs); 414711422SMark.Musante@Sun.COM nvlist_free(nvlist); 41488845Samw@Sun.COM return (error); 41498845Samw@Sun.COM } 41508845Samw@Sun.COM error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target, 41518845Samw@Sun.COM kcred, NULL, 0); 41528845Samw@Sun.COM nvlist_free(nvlist); 41538845Samw@Sun.COM break; 41548845Samw@Sun.COM 41558845Samw@Sun.COM case ZFS_SMB_ACL_PURGE: 41568845Samw@Sun.COM error = zfs_smb_acl_purge(sharedir); 41578845Samw@Sun.COM break; 41588845Samw@Sun.COM 41598845Samw@Sun.COM default: 41608845Samw@Sun.COM error = EINVAL; 41618845Samw@Sun.COM break; 41628845Samw@Sun.COM } 41638845Samw@Sun.COM 41648845Samw@Sun.COM VN_RELE(vp); 41658845Samw@Sun.COM VN_RELE(ZTOV(sharedir)); 41668845Samw@Sun.COM 41678845Samw@Sun.COM ZFS_EXIT(zfsvfs); 41688845Samw@Sun.COM 41698845Samw@Sun.COM return (error); 41708845Samw@Sun.COM } 41718845Samw@Sun.COM 41724543Smarks /* 417310242Schris.kirby@sun.com * inputs: 417410242Schris.kirby@sun.com * zc_name name of filesystem 417510242Schris.kirby@sun.com * zc_value short name of snap 417610242Schris.kirby@sun.com * zc_string user-supplied tag for this reference 417710242Schris.kirby@sun.com * zc_cookie recursive flag 417810342Schris.kirby@sun.com * zc_temphold set if hold is temporary 417910242Schris.kirby@sun.com * 418010242Schris.kirby@sun.com * outputs: none 418110242Schris.kirby@sun.com */ 418210242Schris.kirby@sun.com static int 418310242Schris.kirby@sun.com zfs_ioc_hold(zfs_cmd_t *zc) 418410242Schris.kirby@sun.com { 418510242Schris.kirby@sun.com boolean_t recursive = zc->zc_cookie; 418610242Schris.kirby@sun.com 418710242Schris.kirby@sun.com if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0) 418810242Schris.kirby@sun.com return (EINVAL); 418910242Schris.kirby@sun.com 419010242Schris.kirby@sun.com return (dsl_dataset_user_hold(zc->zc_name, zc->zc_value, 419110342Schris.kirby@sun.com zc->zc_string, recursive, zc->zc_temphold)); 419210242Schris.kirby@sun.com } 419310242Schris.kirby@sun.com 419410242Schris.kirby@sun.com /* 419510242Schris.kirby@sun.com * inputs: 419610242Schris.kirby@sun.com * zc_name name of dataset from which we're releasing a user reference 419710242Schris.kirby@sun.com * zc_value short name of snap 419810242Schris.kirby@sun.com * zc_string user-supplied tag for this reference 419910242Schris.kirby@sun.com * zc_cookie recursive flag 420010242Schris.kirby@sun.com * 420110242Schris.kirby@sun.com * outputs: none 420210242Schris.kirby@sun.com */ 420310242Schris.kirby@sun.com static int 420410242Schris.kirby@sun.com zfs_ioc_release(zfs_cmd_t *zc) 420510242Schris.kirby@sun.com { 420610242Schris.kirby@sun.com boolean_t recursive = zc->zc_cookie; 420710242Schris.kirby@sun.com 420810242Schris.kirby@sun.com if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0) 420910242Schris.kirby@sun.com return (EINVAL); 421010242Schris.kirby@sun.com 421110242Schris.kirby@sun.com return (dsl_dataset_user_release(zc->zc_name, zc->zc_value, 421210242Schris.kirby@sun.com zc->zc_string, recursive)); 421310242Schris.kirby@sun.com } 421410242Schris.kirby@sun.com 421510242Schris.kirby@sun.com /* 421610242Schris.kirby@sun.com * inputs: 421710242Schris.kirby@sun.com * zc_name name of filesystem 421810242Schris.kirby@sun.com * 421910242Schris.kirby@sun.com * outputs: 422010242Schris.kirby@sun.com * zc_nvlist_src{_size} nvlist of snapshot holds 422110242Schris.kirby@sun.com */ 422210242Schris.kirby@sun.com static int 422310242Schris.kirby@sun.com zfs_ioc_get_holds(zfs_cmd_t *zc) 422410242Schris.kirby@sun.com { 422510242Schris.kirby@sun.com nvlist_t *nvp; 422610242Schris.kirby@sun.com int error; 422710242Schris.kirby@sun.com 422810242Schris.kirby@sun.com if ((error = dsl_dataset_get_holds(zc->zc_name, &nvp)) == 0) { 422910242Schris.kirby@sun.com error = put_nvlist(zc, nvp); 423010242Schris.kirby@sun.com nvlist_free(nvp); 423110242Schris.kirby@sun.com } 423210242Schris.kirby@sun.com 423310242Schris.kirby@sun.com return (error); 423410242Schris.kirby@sun.com } 423510242Schris.kirby@sun.com 423610242Schris.kirby@sun.com /* 42374988Sek110237 * pool create, destroy, and export don't log the history as part of 42384988Sek110237 * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export 42394988Sek110237 * do the logging of those commands. 42404543Smarks */ 4241789Sahrens static zfs_ioc_vec_t zfs_ioc_vec[] = { 42429234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE, 42439234SGeorge.Wilson@Sun.COM B_FALSE }, 42449234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_destroy, zfs_secpolicy_config, POOL_NAME, B_FALSE, 42459234SGeorge.Wilson@Sun.COM B_FALSE }, 42469234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE, 42479234SGeorge.Wilson@Sun.COM B_FALSE }, 42489234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE, 42499234SGeorge.Wilson@Sun.COM B_FALSE }, 42509234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_configs, zfs_secpolicy_none, NO_NAME, B_FALSE, 42519234SGeorge.Wilson@Sun.COM B_FALSE }, 42529234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE, 42539234SGeorge.Wilson@Sun.COM B_FALSE }, 42549234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE, 42559234SGeorge.Wilson@Sun.COM B_FALSE }, 42569234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_scrub, zfs_secpolicy_config, POOL_NAME, B_TRUE, 42579234SGeorge.Wilson@Sun.COM B_TRUE }, 42589234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE, 42599234SGeorge.Wilson@Sun.COM B_FALSE }, 42609234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_upgrade, zfs_secpolicy_config, POOL_NAME, B_TRUE, 42619234SGeorge.Wilson@Sun.COM B_TRUE }, 42629234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE, 42639234SGeorge.Wilson@Sun.COM B_FALSE }, 42649234SGeorge.Wilson@Sun.COM { zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE, 42659234SGeorge.Wilson@Sun.COM B_TRUE }, 42669234SGeorge.Wilson@Sun.COM { zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE, 42679234SGeorge.Wilson@Sun.COM B_TRUE }, 42689234SGeorge.Wilson@Sun.COM { zfs_ioc_vdev_set_state, zfs_secpolicy_config, POOL_NAME, B_TRUE, 42699234SGeorge.Wilson@Sun.COM B_FALSE }, 42709234SGeorge.Wilson@Sun.COM { zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE, 42719234SGeorge.Wilson@Sun.COM B_TRUE }, 42729234SGeorge.Wilson@Sun.COM { zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE, 42739234SGeorge.Wilson@Sun.COM B_TRUE }, 42749234SGeorge.Wilson@Sun.COM { zfs_ioc_vdev_setpath, zfs_secpolicy_config, POOL_NAME, B_FALSE, 42759234SGeorge.Wilson@Sun.COM B_TRUE }, 42769425SEric.Schrock@Sun.COM { zfs_ioc_vdev_setfru, zfs_secpolicy_config, POOL_NAME, B_FALSE, 42779425SEric.Schrock@Sun.COM B_TRUE }, 42789234SGeorge.Wilson@Sun.COM { zfs_ioc_objset_stats, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 427911454SSanjeev.Bagewadi@Sun.COM B_TRUE }, 42809234SGeorge.Wilson@Sun.COM { zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 42819234SGeorge.Wilson@Sun.COM B_FALSE }, 42829234SGeorge.Wilson@Sun.COM { zfs_ioc_dataset_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 428311454SSanjeev.Bagewadi@Sun.COM B_TRUE }, 42849234SGeorge.Wilson@Sun.COM { zfs_ioc_snapshot_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 428511454SSanjeev.Bagewadi@Sun.COM B_TRUE }, 42869234SGeorge.Wilson@Sun.COM { zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE, B_TRUE }, 42879234SGeorge.Wilson@Sun.COM { zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE, B_TRUE }, 42889234SGeorge.Wilson@Sun.COM { zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE, 42899234SGeorge.Wilson@Sun.COM B_TRUE}, 42909234SGeorge.Wilson@Sun.COM { zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE, 42919234SGeorge.Wilson@Sun.COM B_TRUE }, 42929234SGeorge.Wilson@Sun.COM { zfs_ioc_rename, zfs_secpolicy_rename, DATASET_NAME, B_TRUE, B_TRUE }, 42939234SGeorge.Wilson@Sun.COM { zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE, B_TRUE }, 42949234SGeorge.Wilson@Sun.COM { zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE, B_FALSE }, 42959234SGeorge.Wilson@Sun.COM { zfs_ioc_inject_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE, 42969234SGeorge.Wilson@Sun.COM B_FALSE }, 42979234SGeorge.Wilson@Sun.COM { zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE, 42989234SGeorge.Wilson@Sun.COM B_FALSE }, 42999234SGeorge.Wilson@Sun.COM { zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE, 43009234SGeorge.Wilson@Sun.COM B_FALSE }, 43019234SGeorge.Wilson@Sun.COM { zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE, 43029234SGeorge.Wilson@Sun.COM B_FALSE }, 43039234SGeorge.Wilson@Sun.COM { zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE, B_FALSE }, 43049234SGeorge.Wilson@Sun.COM { zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE, 43059234SGeorge.Wilson@Sun.COM B_TRUE }, 430611314Swilliam.gorrell@sun.com { zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, DATASET_NAME, 430711314Swilliam.gorrell@sun.com B_TRUE, B_TRUE }, 43089234SGeorge.Wilson@Sun.COM { zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE, 43099234SGeorge.Wilson@Sun.COM B_TRUE }, 43109234SGeorge.Wilson@Sun.COM { zfs_ioc_dsobj_to_dsname, zfs_secpolicy_config, POOL_NAME, B_FALSE, 43119234SGeorge.Wilson@Sun.COM B_FALSE }, 431210233SGeorge.Wilson@Sun.COM { zfs_ioc_obj_to_path, zfs_secpolicy_config, DATASET_NAME, B_FALSE, 431310233SGeorge.Wilson@Sun.COM B_TRUE }, 43149234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_set_props, zfs_secpolicy_config, POOL_NAME, B_TRUE, 43159234SGeorge.Wilson@Sun.COM B_TRUE }, 43169234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE, 43179234SGeorge.Wilson@Sun.COM B_FALSE }, 43189234SGeorge.Wilson@Sun.COM { zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE, 43199234SGeorge.Wilson@Sun.COM B_TRUE }, 43209234SGeorge.Wilson@Sun.COM { zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 43219234SGeorge.Wilson@Sun.COM B_FALSE }, 43229234SGeorge.Wilson@Sun.COM { zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE, B_FALSE }, 43239234SGeorge.Wilson@Sun.COM { zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE, 43249234SGeorge.Wilson@Sun.COM B_TRUE }, 43259234SGeorge.Wilson@Sun.COM { zfs_ioc_smb_acl, zfs_secpolicy_smb_acl, DATASET_NAME, B_FALSE, 43269396SMatthew.Ahrens@Sun.COM B_FALSE }, 43279396SMatthew.Ahrens@Sun.COM { zfs_ioc_userspace_one, zfs_secpolicy_userspace_one, 43289396SMatthew.Ahrens@Sun.COM DATASET_NAME, B_FALSE, B_FALSE }, 43299396SMatthew.Ahrens@Sun.COM { zfs_ioc_userspace_many, zfs_secpolicy_userspace_many, 43309396SMatthew.Ahrens@Sun.COM DATASET_NAME, B_FALSE, B_FALSE }, 43319396SMatthew.Ahrens@Sun.COM { zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade, 43329396SMatthew.Ahrens@Sun.COM DATASET_NAME, B_FALSE, B_TRUE }, 433310242Schris.kirby@sun.com { zfs_ioc_hold, zfs_secpolicy_hold, DATASET_NAME, B_TRUE, B_TRUE }, 433410242Schris.kirby@sun.com { zfs_ioc_release, zfs_secpolicy_release, DATASET_NAME, B_TRUE, 433510242Schris.kirby@sun.com B_TRUE }, 433610242Schris.kirby@sun.com { zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 433711022STom.Erickson@Sun.COM B_TRUE }, 433811022STom.Erickson@Sun.COM { zfs_ioc_objset_recvd_props, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 433911422SMark.Musante@Sun.COM B_FALSE }, 434011422SMark.Musante@Sun.COM { zfs_ioc_vdev_split, zfs_secpolicy_config, POOL_NAME, B_TRUE, 434111422SMark.Musante@Sun.COM B_TRUE } 4342789Sahrens }; 4343789Sahrens 43449234SGeorge.Wilson@Sun.COM int 43459234SGeorge.Wilson@Sun.COM pool_status_check(const char *name, zfs_ioc_namecheck_t type) 43469234SGeorge.Wilson@Sun.COM { 43479234SGeorge.Wilson@Sun.COM spa_t *spa; 43489234SGeorge.Wilson@Sun.COM int error; 43499234SGeorge.Wilson@Sun.COM 43509234SGeorge.Wilson@Sun.COM ASSERT(type == POOL_NAME || type == DATASET_NAME); 43519234SGeorge.Wilson@Sun.COM 43529396SMatthew.Ahrens@Sun.COM error = spa_open(name, &spa, FTAG); 43539234SGeorge.Wilson@Sun.COM if (error == 0) { 43549234SGeorge.Wilson@Sun.COM if (spa_suspended(spa)) 43559234SGeorge.Wilson@Sun.COM error = EAGAIN; 43569234SGeorge.Wilson@Sun.COM spa_close(spa, FTAG); 43579234SGeorge.Wilson@Sun.COM } 43589234SGeorge.Wilson@Sun.COM return (error); 43599234SGeorge.Wilson@Sun.COM } 43609234SGeorge.Wilson@Sun.COM 4361789Sahrens static int 4362789Sahrens zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp) 4363789Sahrens { 4364789Sahrens zfs_cmd_t *zc; 4365789Sahrens uint_t vec; 43662199Sahrens int error, rc; 4367789Sahrens 4368789Sahrens if (getminor(dev) != 0) 4369789Sahrens return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp)); 4370789Sahrens 4371789Sahrens vec = cmd - ZFS_IOC; 43724787Sahrens ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip)); 4373789Sahrens 4374789Sahrens if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0])) 4375789Sahrens return (EINVAL); 4376789Sahrens 4377789Sahrens zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP); 4378789Sahrens 43799643SEric.Taylor@Sun.COM error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag); 438011807SSam.Falkner@Sun.COM if (error != 0) 438111807SSam.Falkner@Sun.COM error = EFAULT; 4382789Sahrens 438310588SEric.Taylor@Sun.COM if ((error == 0) && !(flag & FKIOCTL)) 43844543Smarks error = zfs_ioc_vec[vec].zvec_secpolicy(zc, cr); 4385789Sahrens 4386789Sahrens /* 4387789Sahrens * Ensure that all pool/dataset names are valid before we pass down to 4388789Sahrens * the lower layers. 4389789Sahrens */ 4390789Sahrens if (error == 0) { 4391789Sahrens zc->zc_name[sizeof (zc->zc_name) - 1] = '\0'; 43929643SEric.Taylor@Sun.COM zc->zc_iflags = flag & FKIOCTL; 4393789Sahrens switch (zfs_ioc_vec[vec].zvec_namecheck) { 43944577Sahrens case POOL_NAME: 4395789Sahrens if (pool_namecheck(zc->zc_name, NULL, NULL) != 0) 4396789Sahrens error = EINVAL; 43979234SGeorge.Wilson@Sun.COM if (zfs_ioc_vec[vec].zvec_pool_check) 43989234SGeorge.Wilson@Sun.COM error = pool_status_check(zc->zc_name, 43999234SGeorge.Wilson@Sun.COM zfs_ioc_vec[vec].zvec_namecheck); 4400789Sahrens break; 4401789Sahrens 44024577Sahrens case DATASET_NAME: 4403789Sahrens if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0) 4404789Sahrens error = EINVAL; 44059234SGeorge.Wilson@Sun.COM if (zfs_ioc_vec[vec].zvec_pool_check) 44069234SGeorge.Wilson@Sun.COM error = pool_status_check(zc->zc_name, 44079234SGeorge.Wilson@Sun.COM zfs_ioc_vec[vec].zvec_namecheck); 4408789Sahrens break; 44092856Snd150628 44104577Sahrens case NO_NAME: 44112856Snd150628 break; 4412789Sahrens } 4413789Sahrens } 4414789Sahrens 4415789Sahrens if (error == 0) 4416789Sahrens error = zfs_ioc_vec[vec].zvec_func(zc); 4417789Sahrens 44189643SEric.Taylor@Sun.COM rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag); 44194543Smarks if (error == 0) { 442011807SSam.Falkner@Sun.COM if (rc != 0) 442111807SSam.Falkner@Sun.COM error = EFAULT; 44229396SMatthew.Ahrens@Sun.COM if (zfs_ioc_vec[vec].zvec_his_log) 44234543Smarks zfs_log_history(zc); 44244543Smarks } 4425789Sahrens 4426789Sahrens kmem_free(zc, sizeof (zfs_cmd_t)); 4427789Sahrens return (error); 4428789Sahrens } 4429789Sahrens 4430789Sahrens static int 4431789Sahrens zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 4432789Sahrens { 4433789Sahrens if (cmd != DDI_ATTACH) 4434789Sahrens return (DDI_FAILURE); 4435789Sahrens 4436789Sahrens if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0, 4437789Sahrens DDI_PSEUDO, 0) == DDI_FAILURE) 4438789Sahrens return (DDI_FAILURE); 4439789Sahrens 4440789Sahrens zfs_dip = dip; 4441789Sahrens 4442789Sahrens ddi_report_dev(dip); 4443789Sahrens 4444789Sahrens return (DDI_SUCCESS); 4445789Sahrens } 4446789Sahrens 4447789Sahrens static int 4448789Sahrens zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 4449789Sahrens { 4450789Sahrens if (spa_busy() || zfs_busy() || zvol_busy()) 4451789Sahrens return (DDI_FAILURE); 4452789Sahrens 4453789Sahrens if (cmd != DDI_DETACH) 4454789Sahrens return (DDI_FAILURE); 4455789Sahrens 4456789Sahrens zfs_dip = NULL; 4457789Sahrens 4458789Sahrens ddi_prop_remove_all(dip); 4459789Sahrens ddi_remove_minor_node(dip, NULL); 4460789Sahrens 4461789Sahrens return (DDI_SUCCESS); 4462789Sahrens } 4463789Sahrens 4464789Sahrens /*ARGSUSED*/ 4465789Sahrens static int 4466789Sahrens zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 4467789Sahrens { 4468789Sahrens switch (infocmd) { 4469789Sahrens case DDI_INFO_DEVT2DEVINFO: 4470789Sahrens *result = zfs_dip; 4471789Sahrens return (DDI_SUCCESS); 4472789Sahrens 4473789Sahrens case DDI_INFO_DEVT2INSTANCE: 4474849Sbonwick *result = (void *)0; 4475789Sahrens return (DDI_SUCCESS); 4476789Sahrens } 4477789Sahrens 4478789Sahrens return (DDI_FAILURE); 4479789Sahrens } 4480789Sahrens 4481789Sahrens /* 4482789Sahrens * OK, so this is a little weird. 4483789Sahrens * 4484789Sahrens * /dev/zfs is the control node, i.e. minor 0. 4485789Sahrens * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0. 4486789Sahrens * 4487789Sahrens * /dev/zfs has basically nothing to do except serve up ioctls, 4488789Sahrens * so most of the standard driver entry points are in zvol.c. 4489789Sahrens */ 4490789Sahrens static struct cb_ops zfs_cb_ops = { 4491789Sahrens zvol_open, /* open */ 4492789Sahrens zvol_close, /* close */ 4493789Sahrens zvol_strategy, /* strategy */ 4494789Sahrens nodev, /* print */ 44956423Sgw25295 zvol_dump, /* dump */ 4496789Sahrens zvol_read, /* read */ 4497789Sahrens zvol_write, /* write */ 4498789Sahrens zfsdev_ioctl, /* ioctl */ 4499789Sahrens nodev, /* devmap */ 4500789Sahrens nodev, /* mmap */ 4501789Sahrens nodev, /* segmap */ 4502789Sahrens nochpoll, /* poll */ 4503789Sahrens ddi_prop_op, /* prop_op */ 4504789Sahrens NULL, /* streamtab */ 4505789Sahrens D_NEW | D_MP | D_64BIT, /* Driver compatibility flag */ 4506789Sahrens CB_REV, /* version */ 45073638Sbillm nodev, /* async read */ 45083638Sbillm nodev, /* async write */ 4509789Sahrens }; 4510789Sahrens 4511789Sahrens static struct dev_ops zfs_dev_ops = { 4512789Sahrens DEVO_REV, /* version */ 4513789Sahrens 0, /* refcnt */ 4514789Sahrens zfs_info, /* info */ 4515789Sahrens nulldev, /* identify */ 4516789Sahrens nulldev, /* probe */ 4517789Sahrens zfs_attach, /* attach */ 4518789Sahrens zfs_detach, /* detach */ 4519789Sahrens nodev, /* reset */ 4520789Sahrens &zfs_cb_ops, /* driver operations */ 45217656SSherry.Moore@Sun.COM NULL, /* no bus operations */ 45227656SSherry.Moore@Sun.COM NULL, /* power */ 45237656SSherry.Moore@Sun.COM ddi_quiesce_not_needed, /* quiesce */ 4524789Sahrens }; 4525789Sahrens 4526789Sahrens static struct modldrv zfs_modldrv = { 45277656SSherry.Moore@Sun.COM &mod_driverops, 45287656SSherry.Moore@Sun.COM "ZFS storage pool", 45297656SSherry.Moore@Sun.COM &zfs_dev_ops 4530789Sahrens }; 4531789Sahrens 4532789Sahrens static struct modlinkage modlinkage = { 4533789Sahrens MODREV_1, 4534789Sahrens (void *)&zfs_modlfs, 4535789Sahrens (void *)&zfs_modldrv, 4536789Sahrens NULL 4537789Sahrens }; 4538789Sahrens 45394720Sfr157268 45404720Sfr157268 uint_t zfs_fsyncer_key; 45415326Sek110237 extern uint_t rrw_tsd_key; 45424720Sfr157268 4543789Sahrens int 4544789Sahrens _init(void) 4545789Sahrens { 4546789Sahrens int error; 4547789Sahrens 4548849Sbonwick spa_init(FREAD | FWRITE); 4549849Sbonwick zfs_init(); 4550849Sbonwick zvol_init(); 4551849Sbonwick 4552849Sbonwick if ((error = mod_install(&modlinkage)) != 0) { 4553849Sbonwick zvol_fini(); 4554849Sbonwick zfs_fini(); 4555849Sbonwick spa_fini(); 4556789Sahrens return (error); 4557849Sbonwick } 4558789Sahrens 45594720Sfr157268 tsd_create(&zfs_fsyncer_key, NULL); 45605326Sek110237 tsd_create(&rrw_tsd_key, NULL); 45614720Sfr157268 4562789Sahrens error = ldi_ident_from_mod(&modlinkage, &zfs_li); 4563789Sahrens ASSERT(error == 0); 45644543Smarks mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL); 4565789Sahrens 4566789Sahrens return (0); 4567789Sahrens } 4568789Sahrens 4569789Sahrens int 4570789Sahrens _fini(void) 4571789Sahrens { 4572789Sahrens int error; 4573789Sahrens 45741544Seschrock if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled) 4575789Sahrens return (EBUSY); 4576789Sahrens 4577789Sahrens if ((error = mod_remove(&modlinkage)) != 0) 4578789Sahrens return (error); 4579789Sahrens 4580789Sahrens zvol_fini(); 4581789Sahrens zfs_fini(); 4582789Sahrens spa_fini(); 45835331Samw if (zfs_nfsshare_inited) 45844543Smarks (void) ddi_modclose(nfs_mod); 45855331Samw if (zfs_smbshare_inited) 45865331Samw (void) ddi_modclose(smbsrv_mod); 45875331Samw if (zfs_nfsshare_inited || zfs_smbshare_inited) 45884543Smarks (void) ddi_modclose(sharefs_mod); 4589789Sahrens 45904720Sfr157268 tsd_destroy(&zfs_fsyncer_key); 4591789Sahrens ldi_ident_release(zfs_li); 4592789Sahrens zfs_li = NULL; 45934543Smarks mutex_destroy(&zfs_share_lock); 4594789Sahrens 4595789Sahrens return (error); 4596789Sahrens } 4597789Sahrens 4598789Sahrens int 4599789Sahrens _info(struct modinfo *modinfop) 4600789Sahrens { 4601789Sahrens return (mod_info(&modlinkage, modinfop)); 4602789Sahrens } 4603